Migrating ancient JSON renderer from EDS to UE; sadly does not parse, but I'm working on that in the other one

This commit is contained in:
Michael Becker 2019-09-18 18:56:28 -04:00
parent fd358446df
commit cf735e55e9
14 changed files with 651 additions and 0 deletions

View File

@ -0,0 +1,180 @@
using System;
using System.Collections.Generic;
using UniversalEditor.DataFormats.Text.Plain;
using UniversalEditor.ObjectModels.JSON;
using UniversalEditor.ObjectModels.JSON.Fields;
using UniversalEditor.ObjectModels.Text.Plain;
namespace UniversalEditor.DataFormats.PropertyList.JSON
{
/// <summary>
/// Represents a JSON document, a text-based, human-readable format for representing simple data structures and associative arrays.
/// </summary>
public class JSONDataFormat : PlainTextDataFormat
{
private static DataFormatReference _dfr = null;
protected override DataFormatReference MakeReferenceInternal()
{
if (_dfr == null)
{
_dfr = base.MakeReferenceInternal();
_dfr.Capabilities.Add(typeof(JSONObjectModel), DataFormatCapabilities.All);
}
return _dfr;
}
public void ApplySettings(JSONPresetSettings settings)
{
switch(settings)
{
case JSONPresetSettings.JSON:
mvarSettings.ObjectNamePrefix = "\"";
mvarSettings.ObjectNameSuffix = "\"";
mvarSettings.ObjectNameValueSeparator = ":";
mvarSettings.FieldNamePrefix = "\"";
mvarSettings.FieldNameSuffix = "\"";
mvarSettings.FieldNameValueSeparator = ":";
mvarSettings.FieldSeparator = ",";
mvarSettings.StringLiteralPrefix = "\"";
mvarSettings.StringLiteralSuffix = "\"";
break;
case JSONPresetSettings.ExtendedINI:
mvarSettings.ObjectNamePrefix = "";
mvarSettings.ObjectNameSuffix = "";
mvarSettings.ObjectNameValueSeparator = " ";
mvarSettings.FieldNamePrefix = "";
mvarSettings.FieldNameSuffix = "";
mvarSettings.FieldNameValueSeparator = "=";
mvarSettings.FieldSeparator = ";";
mvarSettings.StringLiteralPrefix = "\"";
mvarSettings.StringLiteralSuffix = "\"";
break;
}
}
private JSONSettings mvarSettings = new JSONSettings();
public JSONSettings Settings { get { return mvarSettings; } }
protected override void BeforeLoadInternal(Stack<ObjectModel> objectModels)
{
base.BeforeLoadInternal(objectModels);
objectModels.Push(new PlainTextObjectModel());
}
protected override void AfterLoadInternal(Stack<ObjectModel> objectModels)
{
base.AfterLoadInternal(objectModels);
PlainTextObjectModel ptom = (objectModels.Pop() as PlainTextObjectModel);
JSONObjectModel json = (objectModels.Pop() as JSONObjectModel);
throw new NotImplementedException();
}
protected override void BeforeSaveInternal(Stack<ObjectModel> objectModels)
{
base.BeforeSaveInternal(objectModels);
JSONObjectModel json = (objectModels.Pop() as JSONObjectModel);
PlainTextObjectModel ptom = (objectModels.Pop() as PlainTextObjectModel);
ptom.Lines.Clear();
for (int iObj = 0; iObj < json.Objects.Count; iObj++)
{
WriteObject(ptom, json.Objects[iObj]);
if (iObj < json.Objects.Count - 1)
ptom.WriteLine();
}
}
private void WriteObject(PlainTextObjectModel ptom, JSONObject obj)
{
WriteObject(ptom, obj, 0);
}
private void WriteObject(PlainTextObjectModel ptom, JSONObject obj, int indentLevel)
{
string szIndent = "";
if (mvarSettings.IndentChildFields)
{
szIndent = ptom.GetIndent(indentLevel);
}
if (obj.Name != "")
{
ptom.Lines.Add(mvarSettings.ObjectNamePrefix + obj.Name + mvarSettings.ObjectNameSuffix + (mvarSettings.AppendSpaceAfterObjectName ? " " : "") + mvarSettings.ObjectNameValueSeparator + " ");
}
if (mvarSettings.AppendLineAfterObjectName && obj.Name != "")
{
ptom.WriteLine();
ptom.Write(szIndent);
}
ptom.WriteLine(mvarSettings.ObjectPrefix);
// Content body
for (int i = 0; i < obj.Fields.Count; i++)
{
JSONField f = obj.Fields[i];
WriteField(ptom, f, indentLevel + 1);
if (i < obj.Fields.Count - 1)
{
ptom.Write(mvarSettings.FieldSeparator);
}
if (mvarSettings.AppendLineAfterField) ptom.WriteLine();
}
ptom.Write(szIndent + mvarSettings.ObjectSuffix);
}
private void WriteField(PlainTextObjectModel ptom, JSONField f, int indentLevel)
{
if (f.GetType() != typeof(JSONObjectField))
{
if (mvarSettings.IndentChildFields)
{
ptom.Write(ptom.GetIndent(indentLevel));
}
ptom.Write(mvarSettings.FieldNamePrefix + f.Name + mvarSettings.FieldNameSuffix + mvarSettings.FieldNameValueSeparator + " ");
if (mvarSettings.AppendLineAfterFieldName)
{
ptom.WriteLine();
ptom.Write(ptom.GetIndent(indentLevel));
}
}
if (f.GetType() == typeof(JSONArrayField))
{
ptom.Write(mvarSettings.ArrayPrefix);
if (mvarSettings.AppendLineAfterStartArray) ptom.WriteLine();
JSONArrayField af = (f as JSONArrayField);
for(int i = 0; i < af.Values.Count; i++)
{
if (mvarSettings.IndentArrayValues)
{
ptom.Write(ptom.GetIndent(indentLevel + 1));
}
ptom.Write(mvarSettings.ArrayValuePrefix + af.Values[i] + mvarSettings.ArrayValueSuffix);
if (i < af.Values.Count - 1) ptom.Write(", ");
if (mvarSettings.AppendLineAfterArrayValue) ptom.WriteLine();
}
ptom.Write(ptom.GetIndent(indentLevel) + mvarSettings.ArraySuffix);
}
else if (f.GetType() == typeof(JSONBooleanField))
{
ptom.Write((f as JSONBooleanField).Value.ToString().ToLower());
}
else if (f.GetType() == typeof(JSONNumberField))
{
ptom.Write((f as JSONNumberField).Value.ToString());
}
else if (f.GetType() == typeof(JSONObjectField))
{
WriteObject(ptom, (f as JSONObjectField).Value, indentLevel);
}
else if (f.GetType() == typeof(JSONStringField))
{
ptom.Write(mvarSettings.StringLiteralPrefix + (f as JSONStringField).Value + mvarSettings.StringLiteralSuffix);
}
}
}
}

View File

@ -0,0 +1,10 @@
using System;
namespace UniversalEditor.DataFormats.PropertyList.JSON
{
public enum JSONPresetSettings
{
JSON = 0,
ExtendedINI = 1
}
}

View File

@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace UniversalEditor.DataFormats.PropertyList.JSON
{
public class JSONSettings
{
private string mvarObjectNameValueSeparator = ":";
public string ObjectNameValueSeparator { get { return mvarObjectNameValueSeparator; } set { mvarObjectNameValueSeparator = value; } }
private string mvarFieldNamePrefix = "\"";
public string FieldNamePrefix { get { return mvarFieldNamePrefix; } set { mvarFieldNamePrefix = value; } }
private string mvarFieldNameSuffix = "\"";
public string FieldNameSuffix { get { return mvarFieldNameSuffix; } set { mvarFieldNameSuffix = value; } }
private string mvarArrayPrefix = "[";
public string ArrayPrefix { get { return mvarArrayPrefix; } set { mvarArrayPrefix = value; } }
private string mvarArraySuffix = "]";
public string ArraySuffix { get { return mvarArraySuffix; } set { mvarArraySuffix = value; } }
private string mvarArrayValuePrefix = "\"";
public string ArrayValuePrefix { get { return mvarArrayValuePrefix; } set { mvarArrayValuePrefix = value; } }
private string mvarArrayValueSuffix = "\"";
public string ArrayValueSuffix { get { return mvarArrayValueSuffix; } set { mvarArrayValueSuffix = value; } }
private bool mvarAppendSpaceAfterObjectName = false;
public bool AppendSpaceAfterObjectName { get { return mvarAppendSpaceAfterObjectName; } set { mvarAppendSpaceAfterObjectName = value; } }
private bool mvarAppendLineAfterObjectName = true;
public bool AppendLineAfterObjectName { get { return mvarAppendLineAfterObjectName; } set { mvarAppendLineAfterObjectName = value; } }
private bool mvarAppendLineAfterStartArray = true;
public bool AppendLineAfterStartArray { get { return mvarAppendLineAfterStartArray; } set { mvarAppendLineAfterStartArray = value; } }
private bool mvarAppendLineAfterArrayValue = true;
public bool AppendLineAfterArrayValue { get { return mvarAppendLineAfterArrayValue; } set { mvarAppendLineAfterArrayValue = value; } }
private bool mvarAppendLineAfterField = true;
public bool AppendLineAfterField { get { return mvarAppendLineAfterField; } set { mvarAppendLineAfterField = value; } }
private bool mvarAppendLineAfterFieldName = false;
public bool AppendLineAfterFieldName { get { return mvarAppendLineAfterFieldName; } set { mvarAppendLineAfterFieldName = value; } }
private string mvarFieldSeparator = ",";
public string FieldSeparator { get { return mvarFieldSeparator; } set { mvarFieldSeparator = value; } }
private bool mvarIndentChildFields = true;
public bool IndentChildFields { get { return mvarIndentChildFields; } set { mvarIndentChildFields = value; } }
private bool mvarIndentArrayValues = true;
public bool IndentArrayValues { get { return mvarIndentArrayValues; } set { mvarIndentArrayValues = value; } }
private string mvarObjectNamePrefix = "\"";
public string ObjectNamePrefix { get { return mvarObjectNamePrefix; } set { mvarObjectNamePrefix = value; } }
private string mvarObjectNameSuffix = "\"";
public string ObjectNameSuffix { get { return mvarObjectNameSuffix; } set { mvarObjectNameSuffix = value; } }
private string mvarObjectPrefix = "{";
public string ObjectPrefix { get { return mvarObjectPrefix; } set { mvarObjectPrefix = value; } }
private string mvarObjectSuffix = "}";
public string ObjectSuffix { get { return mvarObjectSuffix; } set { mvarObjectSuffix = value; } }
private string mvarFieldNameValueSeparator = ":";
public string FieldNameValueSeparator { get { return mvarFieldNameValueSeparator; } set { mvarFieldNameValueSeparator = value; } }
private string mvarStringLiteralPrefix = "\"";
public string StringLiteralPrefix { get { return mvarStringLiteralPrefix; } set { mvarStringLiteralPrefix = value; } }
private string mvarStringLiteralSuffix = "\"";
public string StringLiteralSuffix { get { return mvarStringLiteralSuffix; } set { mvarStringLiteralSuffix = value; } }
}
}

View File

@ -0,0 +1,67 @@
//
// PlainTextDataFormat.cs
//
// Author:
// Mike Becker <alcexhim@gmail.com>
//
// Copyright (c) 2019 Mike Becker
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using UniversalEditor.IO;
using UniversalEditor.ObjectModels.Text.Plain;
namespace UniversalEditor.DataFormats.Text.Plain
{
public class PlainTextDataFormat : DataFormat
{
private static DataFormatReference _dfr = null;
protected override DataFormatReference MakeReferenceInternal()
{
if (_dfr == null)
{
_dfr = base.MakeReferenceInternal();
_dfr.Capabilities.Add(typeof(PlainTextObjectModel), DataFormatCapabilities.All);
}
return _dfr;
}
protected override void LoadInternal(ref ObjectModel objectModel)
{
PlainTextObjectModel ptom = (objectModel as PlainTextObjectModel);
if (ptom == null)
throw new ObjectModelNotSupportedException();
Reader reader = Accessor.Reader;
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
ptom.Lines.Add(line);
}
}
protected override void SaveInternal(ObjectModel objectModel)
{
PlainTextObjectModel ptom = (objectModel as PlainTextObjectModel);
if (ptom == null)
throw new ObjectModelNotSupportedException();
Writer writer = Accessor.Writer;
foreach (string line in ptom.Lines)
{
writer.WriteLine(line);
}
}
}
}

View File

@ -0,0 +1,23 @@
using System;
namespace UniversalEditor.ObjectModels.JSON.Fields
{
/// <summary>
/// Description of ArrayField.
/// </summary>
public class JSONArrayField : JSONField
{
private System.Collections.Specialized.StringCollection mvarValues = new System.Collections.Specialized.StringCollection();
public System.Collections.Specialized.StringCollection Values { get { return mvarValues; } }
public override object Clone()
{
JSONArrayField clone = new JSONArrayField();
foreach (string value in Values)
{
clone.Values.Add(value.Clone() as string);
}
return clone;
}
}
}

View File

@ -0,0 +1,21 @@
using System;
namespace UniversalEditor.ObjectModels.JSON.Fields
{
/// <summary>
/// Description of BooleanField.
/// </summary>
public class JSONBooleanField
: JSONField
{
private bool mvarValue = false;
public bool Value { get { return mvarValue; } set { mvarValue = value; } }
public override object Clone()
{
JSONBooleanField clone = new JSONBooleanField();
clone.Value = Value;
return clone;
}
}
}

View File

@ -0,0 +1,21 @@
using System;
namespace UniversalEditor.ObjectModels.JSON.Fields
{
/// <summary>
/// Description of NumberField.
/// </summary>
public class JSONNumberField
: JSONField
{
private int mvarValue = 0;
public int Value { get { return mvarValue; } set { mvarValue = value; } }
public override object Clone()
{
JSONNumberField clone = new JSONNumberField();
clone.Value = Value;
return clone;
}
}
}

View File

@ -0,0 +1,20 @@
using System;
namespace UniversalEditor.ObjectModels.JSON.Fields
{
/// <summary>
/// Description of ObjectField.
/// </summary>
public class JSONObjectField : JSONField
{
private JSONObject mvarValue = null;
public JSONObject Value { get { return mvarValue; } set { mvarValue = value; } }
public override object Clone()
{
JSONObjectField clone = new JSONObjectField();
clone.Value = (Value.Clone() as JSONObject);
return clone;
}
}
}

View File

@ -0,0 +1,21 @@
using System;
namespace UniversalEditor.ObjectModels.JSON.Fields
{
/// <summary>
/// Description of StringField.
/// </summary>
public class JSONStringField
: JSONField
{
private string mvarValue = "";
public string Value { get { return mvarValue; } set { mvarValue = value; } }
public override object Clone()
{
JSONStringField clone = new JSONStringField();
clone.Value = (Value.Clone() as string);
return clone;
}
}
}

View File

@ -0,0 +1,65 @@
using System;
using UniversalEditor.ObjectModels.JSON.Fields;
namespace UniversalEditor.ObjectModels.JSON
{
/// <summary>
/// Description of Field.
/// </summary>
public abstract class JSONField : ICloneable
{
private string mvarName = "";
public string Name { get { return mvarName; } set { mvarName = value; } }
public class JSONFieldCollection
: System.Collections.ObjectModel.Collection<JSONField>
{
public JSONNumberField Add(string Name, int Value)
{
JSONNumberField f = new JSONNumberField();
f.Name = Name;
f.Value = Value;
base.Add(f);
return f;
}
public JSONStringField Add(string Name, string Value)
{
JSONStringField f = new JSONStringField();
f.Name = Name;
f.Value = Value;
base.Add(f);
return f;
}
public JSONBooleanField Add(string Name, bool Value)
{
JSONBooleanField f = new JSONBooleanField();
f.Name = Name;
f.Value = Value;
base.Add(f);
return f;
}
public JSONArrayField Add(string Name, params string[] Values)
{
JSONArrayField f = new JSONArrayField();
f.Name = Name;
foreach(string s in Values)
{
f.Values.Add(s);
}
base.Add(f);
return f;
}
public JSONObjectField Add(string Name, JSONObject Value)
{
JSONObjectField f = new JSONObjectField();
f.Name = Name;
Value.Name = Name;
f.Value = Value;
base.Add(f);
return f;
}
}
public abstract object Clone();
}
}

View File

@ -0,0 +1,72 @@
using System;
namespace UniversalEditor.ObjectModels.JSON
{
/// <summary>
/// Description of Object.
/// </summary>
public class JSONObject : ICloneable
{
private string mvarName = "";
public string Name { get { return mvarName; } set { mvarName = value; } }
private JSONField.JSONFieldCollection mvarFields = new JSONField.JSONFieldCollection();
public JSONField.JSONFieldCollection Fields { get { return mvarFields; } }
public class JSONObjectCollection
: System.Collections.ObjectModel.Collection<JSONObject>
{
public JSONObject Add()
{
return Add("");
}
public JSONObject Add(string Name)
{
JSONObject o = new JSONObject();
o.Name = Name;
base.Add(o);
return o;
}
public JSONObject this[string Name]
{
get
{
foreach(JSONObject o in this)
{
if (o.Name == Name)
{
return o;
}
}
return null;
}
}
public bool Contains(string Name)
{
return (this[Name] != null);
}
public bool Remove(string Name)
{
JSONObject o = this[Name];
if (o != null)
{
base.Remove(o);
return true;
}
return false;
}
}
public object Clone()
{
JSONObject clone = new JSONObject();
foreach (JSONField field in Fields)
{
clone.Fields.Add(field.Clone() as JSONField);
}
clone.Name = (Name.Clone() as string);
return clone;
}
}
}

View File

@ -0,0 +1,46 @@
//
// JSONObjectModel.cs
//
// Author:
// Mike Becker <alcexhim@gmail.com>
//
// Copyright (c) 2019 Mike Becker
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
namespace UniversalEditor.ObjectModels.JSON
{
public class JSONObjectModel : ObjectModel
{
private JSONObject.JSONObjectCollection mvarObjects = new JSONObject.JSONObjectCollection();
public JSONObject.JSONObjectCollection Objects { get { return mvarObjects; } }
public override void Clear()
{
Objects.Clear();
}
public override void CopyTo(ObjectModel where)
{
JSONObjectModel clone = (where as JSONObjectModel);
if (clone == null)
throw new ObjectModelNotSupportedException();
foreach (JSONObject obj in Objects)
{
clone.Objects.Add(obj.Clone() as JSONObject);
}
}
}
}

View File

@ -51,5 +51,27 @@ namespace UniversalEditor.ObjectModels.Text.Plain
mvarLines.Add(splitt);
}
}
private int indentSize = 4;
public string GetIndent(int length)
{
return new string(' ', indentSize * length);
}
public void Write(string value)
{
if (Lines.Count < 1)
{
Lines.Add(value);
}
else
{
Lines[Lines.Count - 1] = Lines[Lines.Count - 1] + value;
}
}
public void WriteLine(string value = "")
{
Write(value + LineTerminator);
}
}
}

View File

@ -167,6 +167,18 @@
<Compile Include="ObjectModels\PropertyList\IPropertyListContainer.cs" />
<Compile Include="ObjectModels\Binary\BinaryObjectModel.cs" />
<Compile Include="DataFormats\Binary\BinaryDataFormat.cs" />
<Compile Include="DataFormats\PropertyList\JSON\JSONDataFormat.cs" />
<Compile Include="DataFormats\PropertyList\JSON\JSONPresetSettings.cs" />
<Compile Include="DataFormats\PropertyList\JSON\JSONSettings.cs" />
<Compile Include="ObjectModels\JSON\JSONObjectModel.cs" />
<Compile Include="ObjectModels\JSON\JSONField.cs" />
<Compile Include="ObjectModels\JSON\JSONObject.cs" />
<Compile Include="ObjectModels\JSON\Fields\JSONArrayField.cs" />
<Compile Include="ObjectModels\JSON\Fields\JSONBooleanField.cs" />
<Compile Include="ObjectModels\JSON\Fields\JSONNumberField.cs" />
<Compile Include="ObjectModels\JSON\Fields\JSONObjectField.cs" />
<Compile Include="ObjectModels\JSON\Fields\JSONStringField.cs" />
<Compile Include="DataFormats\Text\Plain\PlainTextDataFormat.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\UniversalEditor.Core\UniversalEditor.Core.csproj">
@ -183,6 +195,10 @@
<Folder Include="DataFormats\PropertyList\JavaScriptObjectNotation\" />
<Folder Include="ObjectModels\Binary\" />
<Folder Include="DataFormats\Binary\" />
<Folder Include="DataFormats\PropertyList\JSON\" />
<Folder Include="ObjectModels\JSON\" />
<Folder Include="ObjectModels\JSON\Fields\" />
<Folder Include="DataFormats\Text\Plain\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.