support json serialization

This commit is contained in:
mugisoba
2020-02-04 00:49:17 +09:00
parent 6400c9b054
commit e8b7292dd1
33 changed files with 887 additions and 5 deletions

View File

@@ -270,7 +270,14 @@ class CSharpGenerator : public BaseGenerator {
// to map directly to how they're used in C/C++ and file formats.
// That, and Java Enums are expensive, and not universally liked.
GenComment(enum_def.doc_comment, code_ptr, &comment_config);
if (opts.generate_object_based_api) {
code += "#if ENABLE_JSON_SERIALIZATION\n";
code +=
" "
"[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters."
"StringEnumConverter))]\n";
code += "#endif\n";
}
// In C# this indicates enumeration values can be treated as bit flags.
if (enum_def.attributes.Lookup("bit_flags")) {
code += "[System.FlagsAttribute]\n";
@@ -1274,6 +1281,85 @@ class CSharpGenerator : public BaseGenerator {
code += " }\n";
code += " }\n";
code += "}\n\n";
// JsonConverter
code += "#if ENABLE_JSON_SERIALIZATION\n";
if (enum_def.attributes.Lookup("private")) {
code += "internal ";
} else {
code += "public ";
}
code += "class " + union_name +
"_JsonConverter : Newtonsoft.Json.JsonConverter {\n";
code += " public override bool CanConvert(System.Type objectType) {\n";
code += " return objectType == typeof(" + union_name +
") || objectType == typeof(System.Collections.Generic.List<" +
union_name + ">);\n";
code += " }\n";
code +=
" public override void WriteJson(Newtonsoft.Json.JsonWriter writer, "
"object value, "
"Newtonsoft.Json.JsonSerializer serializer) {\n";
code += " var _olist = value as System.Collections.Generic.List<" +
union_name + ">;\n";
code += " if (_olist != null) {\n";
code += " writer.WriteStartArray();\n";
code +=
" foreach (var _o in _olist) { this.WriteJson(writer, _o, "
"serializer); }\n";
code += " writer.WriteEndArray();\n";
code += " } else {\n";
code += " this.WriteJson(writer, value as " + union_name +
", serializer);\n";
code += " }\n";
code += " }\n";
code += " public void WriteJson(Newtonsoft.Json.JsonWriter writer, " +
union_name +
" _o, "
"Newtonsoft.Json.JsonSerializer serializer) {\n";
code += " if (_o == null) return;\n";
code += " serializer.Serialize(writer, _o.Value);\n";
code += " }\n";
code +=
" public override object ReadJson(Newtonsoft.Json.JsonReader reader, "
"System.Type objectType, "
"object existingValue, Newtonsoft.Json.JsonSerializer serializer) {\n";
code +=
" var _olist = existingValue as System.Collections.Generic.List<" +
union_name + ">;\n";
code += " if (_olist != null) {\n";
code += " for (var _j = 0; _j < _olist.Count; ++_j) {\n";
code += " reader.Read();\n";
code +=
" _olist[_j] = this.ReadJson(reader, _olist[_j], serializer);\n";
code += " }\n";
code += " reader.Read();\n";
code += " return _olist;\n";
code += " } else {\n";
code += " return this.ReadJson(reader, existingValue as " +
union_name + ", serializer);\n";
code += " }\n";
code += " }\n";
code += " public " + union_name +
" ReadJson(Newtonsoft.Json.JsonReader reader, " + union_name +
" _o, Newtonsoft.Json.JsonSerializer serializer) {\n";
code += " if (_o == null) return null;\n";
code += " switch (_o.Type) {\n";
for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
auto &ev = **it;
if (ev.union_type.base_type == BASE_TYPE_NONE) {
code += " default: break;\n";
} else {
auto type_name = GenTypeGet_ObjectAPI(ev.union_type, opts);
code += " case " + enum_def.name + "." + ev.name +
": _o.Value = serializer.Deserialize<" + type_name +
">(reader); break;\n";
}
}
code += " }\n";
code += " return _o;\n";
code += " }\n";
code += "}\n";
code += "#endif\n\n";
}
std::string GenTypeName_ObjectAPI(const std::string &name,
@@ -1794,8 +1880,61 @@ class CSharpGenerator : public BaseGenerator {
if (field.value.type.base_type == BASE_TYPE_UTYPE) continue;
if (field.value.type.element == BASE_TYPE_UTYPE) continue;
auto type_name = GenTypeGet_ObjectAPI(field.value.type, opts);
code += " public " + type_name + " " + MakeCamel(field.name, true) +
" { get; set; }\n";
auto camel_name = MakeCamel(field.name, true);
code += "#if ENABLE_JSON_SERIALIZATION\n";
if (IsUnion(field.value.type)) {
auto utype_name = WrapInNameSpace(*field.value.type.enum_def);
code +=
" [Newtonsoft.Json.JsonProperty(\"" + field.name + "_type\")]\n";
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
code += " private " + utype_name + "[] " + camel_name + "Type {\n";
code += " get {\n";
code += " if (this." + camel_name + " == null) return null;\n";
code += " var _o = new " + utype_name + "[this." + camel_name +
".Count];\n";
code +=
" for (var _j = 0; _j < _o.Length; ++_j) { _o[_j] = this." +
camel_name + "[_j].Type; }\n";
code += " return _o;\n";
code += " }\n";
code += " set {\n";
code += " this." + camel_name + " = new List<" + utype_name +
"Union>();\n";
code += " for (var _j = 0; _j < value.Length; ++_j) {\n";
code += " var _o = new " + utype_name + "Union();\n";
code += " _o.Type = value[_j];\n";
code += " this." + camel_name + ".Add(_o);\n";
code += " }\n";
code += " }\n";
code += " }\n";
} else {
code += " private " + utype_name + " " + camel_name + "Type {\n";
code += " get {\n";
code += " return this." + camel_name + " != null ? this." +
camel_name + ".Type : " + utype_name + ".NONE;\n";
code += " }\n";
code += " set {\n";
code += " this." + camel_name + " = new " + utype_name +
"Union();\n";
code += " this." + camel_name + ".Type = value;\n";
code += " }\n";
code += " }\n";
}
}
code += " [Newtonsoft.Json.JsonProperty(\"" + field.name + "\")]\n";
if (IsUnion(field.value.type)) {
auto union_name =
(field.value.type.base_type == BASE_TYPE_VECTOR)
? GenTypeGet_ObjectAPI(field.value.type.VectorType(), opts)
: type_name;
code += " [Newtonsoft.Json.JsonConverter(typeof(" + union_name +
"_JsonConverter))]\n";
}
if (field.attributes.Lookup("hash")) {
code += " [Newtonsoft.Json.JsonIgnore()]\n";
}
code += "#endif\n";
code += " public " + type_name + " " + camel_name + " { get; set; }\n";
}
// Generate Constructor
code += "\n";
@@ -1833,6 +1972,22 @@ class CSharpGenerator : public BaseGenerator {
}
}
code += " }\n";
// Generate Serialization
if (parser_.root_struct_def_ == &struct_def) {
code += "\n";
code += "#if ENABLE_JSON_SERIALIZATION\n";
code += " public static " + class_name +
" DeserializeFromJson(string jsonText) {\n";
code += " return Newtonsoft.Json.JsonConvert.DeserializeObject<" +
class_name + ">(jsonText);\n";
code += " }\n";
code += " public string SerializeToJson() {\n";
code +=
" return Newtonsoft.Json.JsonConvert.SerializeObject(this, "
"Newtonsoft.Json.Formatting.Indented);\n";
code += " }\n";
code += "#endif\n";
}
code += "}\n\n";
}

View File

@@ -17,7 +17,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>TRACE;DEBUG;ENABLE_JSON_SERIALIZATION</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
@@ -25,7 +25,7 @@
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>TRACE;ENABLE_JSON_SERIALIZATION</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
@@ -37,6 +37,9 @@
<DefineConstants>$(DefineConstants);UNSAFE_BYTEBUFFER</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.12.0.3\lib\net35\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
@@ -169,6 +172,11 @@
<Link>Resources\monsterdata_test.mon</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\monsterdata_test.json">
<Link>Resources\monsterdata_test.json</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@@ -287,6 +287,62 @@ namespace FlatBuffers.Test
TestObjectAPI(Monster.GetRootAsMonster(bb));
}
#if ENABLE_JSON_SERIALIZATION
[FlatBuffersTestMethod]
public void CanReadJsonFile()
{
var jsonText = File.ReadAllText(@"Resources/monsterdata_test.json");
var mon = MonsterT.DeserializeFromJson(jsonText);
Assert.AreEqual(1.0f, mon.Pos.X);
Assert.AreEqual(2.0f, mon.Pos.Y);
Assert.AreEqual(3.0f, mon.Pos.Z);
Assert.AreEqual(3.0, mon.Pos.Test1);
Assert.AreEqual(Color.Green, mon.Pos.Test2);
Assert.AreEqual(5, mon.Pos.Test3.A);
Assert.AreEqual(6, mon.Pos.Test3.B);
Assert.AreEqual(80, mon.Hp);
Assert.AreEqual("MyMonster", mon.Name);
Assert.AreEqual(5, mon.Inventory.Count);
Assert.AreEqual(0, mon.Inventory[0]);
Assert.AreEqual(1, mon.Inventory[1]);
Assert.AreEqual(2, mon.Inventory[2]);
Assert.AreEqual(3, mon.Inventory[3]);
Assert.AreEqual(4, mon.Inventory[4]);
Assert.AreEqual(5, mon.VectorOfLongs.Count);
Assert.AreEqual(1L, mon.VectorOfLongs[0]);
Assert.AreEqual(100L, mon.VectorOfLongs[1]);
Assert.AreEqual(10000L, mon.VectorOfLongs[2]);
Assert.AreEqual(1000000L, mon.VectorOfLongs[3]);
Assert.AreEqual(100000000L, mon.VectorOfLongs[4]);
Assert.AreEqual(3, mon.VectorOfDoubles.Count);
Assert.AreEqual(-1.7976931348623157e+308, mon.VectorOfDoubles[0]);
Assert.AreEqual(0.0, mon.VectorOfDoubles[1]);
Assert.AreEqual(1.7976931348623157e+308, mon.VectorOfDoubles[2]);
Assert.AreEqual(Any.Monster, mon.Test.Type);
Assert.AreEqual("Fred", mon.Test.AsMonster().Name);
Assert.IsTrue(null == mon.Test.AsMonster().Pos);
Assert.AreEqual(2, mon.Test4.Count);
Assert.AreEqual(10, mon.Test4[0].A);
Assert.AreEqual(20, mon.Test4[0].B);
Assert.AreEqual(30, mon.Test4[1].A);
Assert.AreEqual(40, mon.Test4[1].B);
Assert.AreEqual(10, mon.Test5[0].A);
Assert.AreEqual(20, mon.Test5[0].B);
Assert.AreEqual(30, mon.Test5[1].A);
Assert.AreEqual(40, mon.Test5[1].B);
Assert.AreEqual(2, mon.Testarrayofstring.Count);
Assert.AreEqual("test1", mon.Testarrayofstring[0]);
Assert.AreEqual("test2", mon.Testarrayofstring[1]);
Assert.AreEqual("Fred", mon.Enemy.Name);
Assert.AreEqual(3, mon.Testarrayofbools.Count);
Assert.AreEqual(true, mon.Testarrayofbools[0]);
Assert.AreEqual(false, mon.Testarrayofbools[1]);
Assert.AreEqual(true, mon.Testarrayofbools[2]);
Assert.AreEqual(true, mon.Testbool);
}
#endif
[FlatBuffersTestMethod]
public void TestEnums()
{
@@ -659,6 +715,12 @@ namespace FlatBuffers.Test
fbb.Finish(Monster.Pack(fbb, b).Value);
var c = Monster.GetRootAsMonster(fbb.DataBuffer);
AreEqual(a, c);
#if ENABLE_JSON_SERIALIZATION
var jsonText = b.SerializeToJson();
var d = MonsterT.DeserializeFromJson(jsonText);
AreEqual(a, d);
#endif
}
private void AreEqual(ArrayTable a, ArrayTableT b)
@@ -754,6 +816,12 @@ namespace FlatBuffers.Test
fbb.Finish(ArrayTable.Pack(fbb, b).Value);
var c = ArrayTable.GetRootAsArrayTable(fbb.DataBuffer);
AreEqual(a, c);
#if ENABLE_JSON_SERIALIZATION
var jsonText = b.SerializeToJson();
var d = ArrayTableT.DeserializeFromJson(jsonText);
AreEqual(a, d);
#endif
}
private void AreEqual(Movie a, MovieT b)
@@ -793,6 +861,12 @@ namespace FlatBuffers.Test
fbb.Finish(Movie.Pack(fbb, b).Value);
var c = Movie.GetRootAsMovie(fbb.DataBuffer);
AreEqual(a, c);
#if ENABLE_JSON_SERIALIZATION
var jsonText = b.SerializeToJson();
var d = MovieT.DeserializeFromJson(jsonText);
AreEqual(a, d);
#endif
}
}
}

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net35" />
</packages>

View File

@@ -47,7 +47,13 @@ public struct Ability : IFlatbufferObject
public class AbilityT
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("id")]
#endif
public uint Id { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("distance")]
#endif
public uint Distance { get; set; }
public AbilityT() {

View File

@@ -5,6 +5,9 @@
namespace MyGame.Example
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
#endif
public enum Any : byte
{
NONE = 0,
@@ -37,5 +40,50 @@ public class AnyUnion {
}
}
#if ENABLE_JSON_SERIALIZATION
public class AnyUnion_JsonConverter : Newtonsoft.Json.JsonConverter {
public override bool CanConvert(System.Type objectType) {
return objectType == typeof(AnyUnion) || objectType == typeof(System.Collections.Generic.List<AnyUnion>);
}
public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) {
var _olist = value as System.Collections.Generic.List<AnyUnion>;
if (_olist != null) {
writer.WriteStartArray();
foreach (var _o in _olist) { this.WriteJson(writer, _o, serializer); }
writer.WriteEndArray();
} else {
this.WriteJson(writer, value as AnyUnion, serializer);
}
}
public void WriteJson(Newtonsoft.Json.JsonWriter writer, AnyUnion _o, Newtonsoft.Json.JsonSerializer serializer) {
if (_o == null) return;
serializer.Serialize(writer, _o.Value);
}
public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) {
var _olist = existingValue as System.Collections.Generic.List<AnyUnion>;
if (_olist != null) {
for (var _j = 0; _j < _olist.Count; ++_j) {
reader.Read();
_olist[_j] = this.ReadJson(reader, _olist[_j], serializer);
}
reader.Read();
return _olist;
} else {
return this.ReadJson(reader, existingValue as AnyUnion, serializer);
}
}
public AnyUnion ReadJson(Newtonsoft.Json.JsonReader reader, AnyUnion _o, Newtonsoft.Json.JsonSerializer serializer) {
if (_o == null) return null;
switch (_o.Type) {
default: break;
case Any.Monster: _o.Value = serializer.Deserialize<MyGame.Example.MonsterT>(reader); break;
case Any.TestSimpleTableWithEnum: _o.Value = serializer.Deserialize<MyGame.Example.TestSimpleTableWithEnumT>(reader); break;
case Any.MyGame_Example2_Monster: _o.Value = serializer.Deserialize<MyGame.Example2.MonsterT>(reader); break;
}
return _o;
}
}
#endif
}

View File

@@ -5,6 +5,9 @@
namespace MyGame.Example
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
#endif
public enum AnyAmbiguousAliases : byte
{
NONE = 0,
@@ -37,5 +40,50 @@ public class AnyAmbiguousAliasesUnion {
}
}
#if ENABLE_JSON_SERIALIZATION
public class AnyAmbiguousAliasesUnion_JsonConverter : Newtonsoft.Json.JsonConverter {
public override bool CanConvert(System.Type objectType) {
return objectType == typeof(AnyAmbiguousAliasesUnion) || objectType == typeof(System.Collections.Generic.List<AnyAmbiguousAliasesUnion>);
}
public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) {
var _olist = value as System.Collections.Generic.List<AnyAmbiguousAliasesUnion>;
if (_olist != null) {
writer.WriteStartArray();
foreach (var _o in _olist) { this.WriteJson(writer, _o, serializer); }
writer.WriteEndArray();
} else {
this.WriteJson(writer, value as AnyAmbiguousAliasesUnion, serializer);
}
}
public void WriteJson(Newtonsoft.Json.JsonWriter writer, AnyAmbiguousAliasesUnion _o, Newtonsoft.Json.JsonSerializer serializer) {
if (_o == null) return;
serializer.Serialize(writer, _o.Value);
}
public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) {
var _olist = existingValue as System.Collections.Generic.List<AnyAmbiguousAliasesUnion>;
if (_olist != null) {
for (var _j = 0; _j < _olist.Count; ++_j) {
reader.Read();
_olist[_j] = this.ReadJson(reader, _olist[_j], serializer);
}
reader.Read();
return _olist;
} else {
return this.ReadJson(reader, existingValue as AnyAmbiguousAliasesUnion, serializer);
}
}
public AnyAmbiguousAliasesUnion ReadJson(Newtonsoft.Json.JsonReader reader, AnyAmbiguousAliasesUnion _o, Newtonsoft.Json.JsonSerializer serializer) {
if (_o == null) return null;
switch (_o.Type) {
default: break;
case AnyAmbiguousAliases.M1: _o.Value = serializer.Deserialize<MyGame.Example.MonsterT>(reader); break;
case AnyAmbiguousAliases.M2: _o.Value = serializer.Deserialize<MyGame.Example.MonsterT>(reader); break;
case AnyAmbiguousAliases.M3: _o.Value = serializer.Deserialize<MyGame.Example.MonsterT>(reader); break;
}
return _o;
}
}
#endif
}

View File

@@ -5,6 +5,9 @@
namespace MyGame.Example
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
#endif
public enum AnyUniqueAliases : byte
{
NONE = 0,
@@ -37,5 +40,50 @@ public class AnyUniqueAliasesUnion {
}
}
#if ENABLE_JSON_SERIALIZATION
public class AnyUniqueAliasesUnion_JsonConverter : Newtonsoft.Json.JsonConverter {
public override bool CanConvert(System.Type objectType) {
return objectType == typeof(AnyUniqueAliasesUnion) || objectType == typeof(System.Collections.Generic.List<AnyUniqueAliasesUnion>);
}
public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) {
var _olist = value as System.Collections.Generic.List<AnyUniqueAliasesUnion>;
if (_olist != null) {
writer.WriteStartArray();
foreach (var _o in _olist) { this.WriteJson(writer, _o, serializer); }
writer.WriteEndArray();
} else {
this.WriteJson(writer, value as AnyUniqueAliasesUnion, serializer);
}
}
public void WriteJson(Newtonsoft.Json.JsonWriter writer, AnyUniqueAliasesUnion _o, Newtonsoft.Json.JsonSerializer serializer) {
if (_o == null) return;
serializer.Serialize(writer, _o.Value);
}
public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) {
var _olist = existingValue as System.Collections.Generic.List<AnyUniqueAliasesUnion>;
if (_olist != null) {
for (var _j = 0; _j < _olist.Count; ++_j) {
reader.Read();
_olist[_j] = this.ReadJson(reader, _olist[_j], serializer);
}
reader.Read();
return _olist;
} else {
return this.ReadJson(reader, existingValue as AnyUniqueAliasesUnion, serializer);
}
}
public AnyUniqueAliasesUnion ReadJson(Newtonsoft.Json.JsonReader reader, AnyUniqueAliasesUnion _o, Newtonsoft.Json.JsonSerializer serializer) {
if (_o == null) return null;
switch (_o.Type) {
default: break;
case AnyUniqueAliases.M: _o.Value = serializer.Deserialize<MyGame.Example.MonsterT>(reader); break;
case AnyUniqueAliases.TS: _o.Value = serializer.Deserialize<MyGame.Example.TestSimpleTableWithEnumT>(reader); break;
case AnyUniqueAliases.M2: _o.Value = serializer.Deserialize<MyGame.Example2.MonsterT>(reader); break;
}
return _o;
}
}
#endif
}

View File

@@ -101,11 +101,29 @@ public struct ArrayStruct : IFlatbufferObject
public class ArrayStructT
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("a")]
#endif
public float A { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("b")]
#endif
public int[] B { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("c")]
#endif
public sbyte C { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("d")]
#endif
public MyGame.Example.NestedStructT[] D { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("e")]
#endif
public int E { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("f")]
#endif
public long[] F { get; set; }
public ArrayStructT() {

View File

@@ -48,11 +48,23 @@ public struct ArrayTable : IFlatbufferObject
public class ArrayTableT
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("a")]
#endif
public MyGame.Example.ArrayStructT A { get; set; }
public ArrayTableT() {
this.A = new MyGame.Example.ArrayStructT();
}
#if ENABLE_JSON_SERIALIZATION
public static ArrayTableT DeserializeFromJson(string jsonText) {
return Newtonsoft.Json.JsonConvert.DeserializeObject<ArrayTableT>(jsonText);
}
public string SerializeToJson() {
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
}
#endif
}

View File

@@ -6,6 +6,9 @@ namespace MyGame.Example
{
/// Composite components of Monster color.
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
#endif
[System.FlagsAttribute]
public enum Color : byte
{

View File

@@ -593,50 +593,232 @@ public struct Monster : IFlatbufferObject
public class MonsterT
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("pos")]
#endif
public MyGame.Example.Vec3T Pos { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("mana")]
#endif
public short Mana { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("hp")]
#endif
public short Hp { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("name")]
#endif
public string Name { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("inventory")]
#endif
public List<byte> Inventory { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("color")]
#endif
public MyGame.Example.Color Color { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("test_type")]
private MyGame.Example.Any TestType {
get {
return this.Test != null ? this.Test.Type : MyGame.Example.Any.NONE;
}
set {
this.Test = new MyGame.Example.AnyUnion();
this.Test.Type = value;
}
}
[Newtonsoft.Json.JsonProperty("test")]
[Newtonsoft.Json.JsonConverter(typeof(MyGame.Example.AnyUnion_JsonConverter))]
#endif
public MyGame.Example.AnyUnion Test { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("test4")]
#endif
public List<MyGame.Example.TestT> Test4 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("testarrayofstring")]
#endif
public List<string> Testarrayofstring { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("testarrayoftables")]
#endif
public List<MyGame.Example.MonsterT> Testarrayoftables { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("enemy")]
#endif
public MyGame.Example.MonsterT Enemy { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("testnestedflatbuffer")]
#endif
public List<byte> Testnestedflatbuffer { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("testempty")]
#endif
public MyGame.Example.StatT Testempty { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("testbool")]
#endif
public bool Testbool { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("testhashs32_fnv1")]
[Newtonsoft.Json.JsonIgnore()]
#endif
public int Testhashs32Fnv1 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("testhashu32_fnv1")]
[Newtonsoft.Json.JsonIgnore()]
#endif
public uint Testhashu32Fnv1 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("testhashs64_fnv1")]
[Newtonsoft.Json.JsonIgnore()]
#endif
public long Testhashs64Fnv1 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("testhashu64_fnv1")]
[Newtonsoft.Json.JsonIgnore()]
#endif
public ulong Testhashu64Fnv1 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("testhashs32_fnv1a")]
[Newtonsoft.Json.JsonIgnore()]
#endif
public int Testhashs32Fnv1a { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("testhashu32_fnv1a")]
[Newtonsoft.Json.JsonIgnore()]
#endif
public uint Testhashu32Fnv1a { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("testhashs64_fnv1a")]
[Newtonsoft.Json.JsonIgnore()]
#endif
public long Testhashs64Fnv1a { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("testhashu64_fnv1a")]
[Newtonsoft.Json.JsonIgnore()]
#endif
public ulong Testhashu64Fnv1a { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("testarrayofbools")]
#endif
public List<bool> Testarrayofbools { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("testf")]
#endif
public float Testf { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("testf2")]
#endif
public float Testf2 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("testf3")]
#endif
public float Testf3 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("testarrayofstring2")]
#endif
public List<string> Testarrayofstring2 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("testarrayofsortedstruct")]
#endif
public List<MyGame.Example.AbilityT> Testarrayofsortedstruct { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("flex")]
#endif
public List<byte> Flex { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("test5")]
#endif
public List<MyGame.Example.TestT> Test5 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("vector_of_longs")]
#endif
public List<long> VectorOfLongs { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("vector_of_doubles")]
#endif
public List<double> VectorOfDoubles { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("parent_namespace_test")]
#endif
public MyGame.InParentNamespaceT ParentNamespaceTest { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("vector_of_referrables")]
#endif
public List<MyGame.Example.ReferrableT> VectorOfReferrables { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("single_weak_reference")]
[Newtonsoft.Json.JsonIgnore()]
#endif
public ulong SingleWeakReference { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("vector_of_weak_references")]
[Newtonsoft.Json.JsonIgnore()]
#endif
public List<ulong> VectorOfWeakReferences { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("vector_of_strong_referrables")]
#endif
public List<MyGame.Example.ReferrableT> VectorOfStrongReferrables { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("co_owning_reference")]
[Newtonsoft.Json.JsonIgnore()]
#endif
public ulong CoOwningReference { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("vector_of_co_owning_references")]
[Newtonsoft.Json.JsonIgnore()]
#endif
public List<ulong> VectorOfCoOwningReferences { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("non_owning_reference")]
[Newtonsoft.Json.JsonIgnore()]
#endif
public ulong NonOwningReference { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("vector_of_non_owning_references")]
[Newtonsoft.Json.JsonIgnore()]
#endif
public List<ulong> VectorOfNonOwningReferences { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("any_unique_type")]
private MyGame.Example.AnyUniqueAliases AnyUniqueType {
get {
return this.AnyUnique != null ? this.AnyUnique.Type : MyGame.Example.AnyUniqueAliases.NONE;
}
set {
this.AnyUnique = new MyGame.Example.AnyUniqueAliasesUnion();
this.AnyUnique.Type = value;
}
}
[Newtonsoft.Json.JsonProperty("any_unique")]
[Newtonsoft.Json.JsonConverter(typeof(MyGame.Example.AnyUniqueAliasesUnion_JsonConverter))]
#endif
public MyGame.Example.AnyUniqueAliasesUnion AnyUnique { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("any_ambiguous_type")]
private MyGame.Example.AnyAmbiguousAliases AnyAmbiguousType {
get {
return this.AnyAmbiguous != null ? this.AnyAmbiguous.Type : MyGame.Example.AnyAmbiguousAliases.NONE;
}
set {
this.AnyAmbiguous = new MyGame.Example.AnyAmbiguousAliasesUnion();
this.AnyAmbiguous.Type = value;
}
}
[Newtonsoft.Json.JsonProperty("any_ambiguous")]
[Newtonsoft.Json.JsonConverter(typeof(MyGame.Example.AnyAmbiguousAliasesUnion_JsonConverter))]
#endif
public MyGame.Example.AnyAmbiguousAliasesUnion AnyAmbiguous { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("vector_of_enums")]
#endif
public List<MyGame.Example.Color> VectorOfEnums { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("signed_enum")]
#endif
public MyGame.Example.Race SignedEnum { get; set; }
public MonsterT() {
@@ -686,6 +868,15 @@ public class MonsterT
this.VectorOfEnums = null;
this.SignedEnum = MyGame.Example.Race.None;
}
#if ENABLE_JSON_SERIALIZATION
public static MonsterT DeserializeFromJson(string jsonText) {
return Newtonsoft.Json.JsonConvert.DeserializeObject<MonsterT>(jsonText);
}
public string SerializeToJson() {
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
}
#endif
}

View File

@@ -70,9 +70,21 @@ public struct NestedStruct : IFlatbufferObject
public class NestedStructT
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("a")]
#endif
public int[] A { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("b")]
#endif
public MyGame.Example.TestEnum B { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("c")]
#endif
public MyGame.Example.TestEnum[] C { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("d")]
#endif
public long[] D { get; set; }
public NestedStructT() {

View File

@@ -5,6 +5,9 @@
namespace MyGame.Example
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
#endif
public enum Race : sbyte
{
None = -1,

View File

@@ -78,6 +78,10 @@ public struct Referrable : IFlatbufferObject
public class ReferrableT
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("id")]
[Newtonsoft.Json.JsonIgnore()]
#endif
public ulong Id { get; set; }
public ReferrableT() {

View File

@@ -73,8 +73,17 @@ public struct Stat : IFlatbufferObject
public class StatT
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("id")]
#endif
public string Id { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("val")]
#endif
public long Val { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("count")]
#endif
public ushort Count { get; set; }
public StatT() {

View File

@@ -48,7 +48,13 @@ public struct Test : IFlatbufferObject
public class TestT
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("a")]
#endif
public short A { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("b")]
#endif
public sbyte B { get; set; }
public TestT() {

View File

@@ -5,6 +5,9 @@
namespace MyGame.Example
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
#endif
public enum TestEnum : sbyte
{
A = 0,

View File

@@ -53,6 +53,9 @@ internal partial struct TestSimpleTableWithEnum : IFlatbufferObject
internal partial class TestSimpleTableWithEnumT
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("color")]
#endif
public MyGame.Example.Color Color { get; set; }
public TestSimpleTableWithEnumT() {

View File

@@ -162,17 +162,53 @@ public struct TypeAliases : IFlatbufferObject
public class TypeAliasesT
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("i8")]
#endif
public sbyte I8 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("u8")]
#endif
public byte U8 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("i16")]
#endif
public short I16 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("u16")]
#endif
public ushort U16 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("i32")]
#endif
public int I32 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("u32")]
#endif
public uint U32 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("i64")]
#endif
public long I64 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("u64")]
#endif
public ulong U64 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("f32")]
#endif
public float F32 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("f64")]
#endif
public double F64 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("v8")]
#endif
public List<sbyte> V8 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("vf64")]
#endif
public List<double> Vf64 { get; set; }
public TypeAliasesT() {

View File

@@ -73,11 +73,29 @@ public struct Vec3 : IFlatbufferObject
public class Vec3T
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("x")]
#endif
public float X { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("y")]
#endif
public float Y { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("z")]
#endif
public float Z { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("test1")]
#endif
public double Test1 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("test2")]
#endif
public MyGame.Example.Color Test2 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("test3")]
#endif
public MyGame.Example.TestT Test3 { get; set; }
public Vec3T() {

View File

@@ -151,15 +151,45 @@ public struct MonsterExtra : IFlatbufferObject
public class MonsterExtraT
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("d0")]
#endif
public double D0 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("d1")]
#endif
public double D1 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("d2")]
#endif
public double D2 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("d3")]
#endif
public double D3 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("f0")]
#endif
public float F0 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("f1")]
#endif
public float F1 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("f2")]
#endif
public float F2 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("f3")]
#endif
public float F3 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("dvec")]
#endif
public List<double> Dvec { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("fvec")]
#endif
public List<float> Fvec { get; set; }
public MonsterExtraT() {
@@ -174,6 +204,15 @@ public class MonsterExtraT
this.Dvec = null;
this.Fvec = null;
}
#if ENABLE_JSON_SERIALIZATION
public static MonsterExtraT DeserializeFromJson(string jsonText) {
return Newtonsoft.Json.JsonConvert.DeserializeObject<MonsterExtraT>(jsonText);
}
public string SerializeToJson() {
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
}
#endif
}

View File

@@ -5,6 +5,9 @@
namespace NamespaceA.NamespaceB
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
#endif
public enum EnumInNestedNS : sbyte
{
A = 0,

View File

@@ -47,7 +47,13 @@ public struct StructInNestedNS : IFlatbufferObject
public class StructInNestedNST
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("a")]
#endif
public int A { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("b")]
#endif
public int B { get; set; }
public StructInNestedNST() {

View File

@@ -53,6 +53,9 @@ public struct TableInNestedNS : IFlatbufferObject
public class TableInNestedNST
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("foo")]
#endif
public int Foo { get; set; }
public TableInNestedNST() {

View File

@@ -53,6 +53,9 @@ public struct SecondTableInA : IFlatbufferObject
public class SecondTableInAT
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("refer_to_c")]
#endif
public NamespaceC.TableInCT ReferToC { get; set; }
public SecondTableInAT() {

View File

@@ -55,8 +55,17 @@ public struct TableInFirstNS : IFlatbufferObject
public class TableInFirstNST
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("foo_table")]
#endif
public NamespaceA.NamespaceB.TableInNestedNST FooTable { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("foo_enum")]
#endif
public NamespaceA.NamespaceB.EnumInNestedNS FooEnum { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("foo_struct")]
#endif
public NamespaceA.NamespaceB.StructInNestedNST FooStruct { get; set; }
public TableInFirstNST() {

View File

@@ -60,7 +60,13 @@ public struct TableInC : IFlatbufferObject
public class TableInCT
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("refer_to_a1")]
#endif
public NamespaceA.TableInFirstNST ReferToA1 { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("refer_to_a2")]
#endif
public NamespaceA.SecondTableInAT ReferToA2 { get; set; }
public TableInCT() {

View File

@@ -50,6 +50,9 @@ public struct Attacker : IFlatbufferObject
public class AttackerT
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("sword_attack_damage")]
#endif
public int SwordAttackDamage { get; set; }
public AttackerT() {

View File

@@ -39,6 +39,9 @@ public struct BookReader : IFlatbufferObject
public class BookReaderT
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("books_read")]
#endif
public int BooksRead { get; set; }
public BookReaderT() {

View File

@@ -2,6 +2,9 @@
// automatically generated by the FlatBuffers compiler, do not modify
// </auto-generated>
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
#endif
public enum Character : byte
{
NONE = 0,
@@ -43,3 +46,51 @@ public class CharacterUnion {
}
}
#if ENABLE_JSON_SERIALIZATION
public class CharacterUnion_JsonConverter : Newtonsoft.Json.JsonConverter {
public override bool CanConvert(System.Type objectType) {
return objectType == typeof(CharacterUnion) || objectType == typeof(System.Collections.Generic.List<CharacterUnion>);
}
public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) {
var _olist = value as System.Collections.Generic.List<CharacterUnion>;
if (_olist != null) {
writer.WriteStartArray();
foreach (var _o in _olist) { this.WriteJson(writer, _o, serializer); }
writer.WriteEndArray();
} else {
this.WriteJson(writer, value as CharacterUnion, serializer);
}
}
public void WriteJson(Newtonsoft.Json.JsonWriter writer, CharacterUnion _o, Newtonsoft.Json.JsonSerializer serializer) {
if (_o == null) return;
serializer.Serialize(writer, _o.Value);
}
public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) {
var _olist = existingValue as System.Collections.Generic.List<CharacterUnion>;
if (_olist != null) {
for (var _j = 0; _j < _olist.Count; ++_j) {
reader.Read();
_olist[_j] = this.ReadJson(reader, _olist[_j], serializer);
}
reader.Read();
return _olist;
} else {
return this.ReadJson(reader, existingValue as CharacterUnion, serializer);
}
}
public CharacterUnion ReadJson(Newtonsoft.Json.JsonReader reader, CharacterUnion _o, Newtonsoft.Json.JsonSerializer serializer) {
if (_o == null) return null;
switch (_o.Type) {
default: break;
case Character.MuLan: _o.Value = serializer.Deserialize<AttackerT>(reader); break;
case Character.Rapunzel: _o.Value = serializer.Deserialize<RapunzelT>(reader); break;
case Character.Belle: _o.Value = serializer.Deserialize<BookReaderT>(reader); break;
case Character.BookFan: _o.Value = serializer.Deserialize<BookReaderT>(reader); break;
case Character.Other: _o.Value = serializer.Deserialize<string>(reader); break;
case Character.Unused: _o.Value = serializer.Deserialize<string>(reader); break;
}
return _o;
}
}
#endif

View File

@@ -146,12 +146,56 @@ public struct Movie : IFlatbufferObject
public class MovieT
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("main_character_type")]
private Character MainCharacterType {
get {
return this.MainCharacter != null ? this.MainCharacter.Type : Character.NONE;
}
set {
this.MainCharacter = new CharacterUnion();
this.MainCharacter.Type = value;
}
}
[Newtonsoft.Json.JsonProperty("main_character")]
[Newtonsoft.Json.JsonConverter(typeof(CharacterUnion_JsonConverter))]
#endif
public CharacterUnion MainCharacter { get; set; }
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("characters_type")]
private Character[] CharactersType {
get {
if (this.Characters == null) return null;
var _o = new Character[this.Characters.Count];
for (var _j = 0; _j < _o.Length; ++_j) { _o[_j] = this.Characters[_j].Type; }
return _o;
}
set {
this.Characters = new List<CharacterUnion>();
for (var _j = 0; _j < value.Length; ++_j) {
var _o = new CharacterUnion();
_o.Type = value[_j];
this.Characters.Add(_o);
}
}
}
[Newtonsoft.Json.JsonProperty("characters")]
[Newtonsoft.Json.JsonConverter(typeof(CharacterUnion_JsonConverter))]
#endif
public List<CharacterUnion> Characters { get; set; }
public MovieT() {
this.MainCharacter = null;
this.Characters = null;
}
#if ENABLE_JSON_SERIALIZATION
public static MovieT DeserializeFromJson(string jsonText) {
return Newtonsoft.Json.JsonConvert.DeserializeObject<MovieT>(jsonText);
}
public string SerializeToJson() {
return Newtonsoft.Json.JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented);
}
#endif
}

View File

@@ -39,6 +39,9 @@ public struct Rapunzel : IFlatbufferObject
public class RapunzelT
{
#if ENABLE_JSON_SERIALIZATION
[Newtonsoft.Json.JsonProperty("hair_length")]
#endif
public int HairLength { get; set; }
public RapunzelT() {