[C#] support Json Serialization (#5752)

* support json serialization

* fix invalid json format.
* string must be written with double quotes.
* remove commma after the last object member.

* fix indent

* Revert "fix invalid json format."

This reverts commit d6820ed50c.

* quated string value.

* add cs-gen-json-serializer flag.

* fix preprocessor indent

* ENABLE_JSON_SERIALIZATION -> ENABLE_JSON_SERIALIZATION_TEST

* share TestBuffer method

* remove ENABLE_JSON_SERIALIZATION

* remove duplicated test data

* [windows] add nuget restore and copy test data.

* [docker mono] share msbuild settings with windows. add nuget restore and copy test data.

* add some note for json api.
This commit is contained in:
mugisoba
2020-02-11 10:43:36 +09:00
committed by GitHub
parent 8f56990f6c
commit 173e10fdf1
43 changed files with 680 additions and 42 deletions

View File

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

View File

@@ -5,6 +5,7 @@
namespace MyGame.Example
{
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public enum Any : byte
{
NONE = 0,
@@ -37,5 +38,48 @@ public class AnyUnion {
}
}
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;
}
}
}

View File

@@ -5,6 +5,7 @@
namespace MyGame.Example
{
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public enum AnyAmbiguousAliases : byte
{
NONE = 0,
@@ -37,5 +38,48 @@ public class AnyAmbiguousAliasesUnion {
}
}
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;
}
}
}

View File

@@ -5,6 +5,7 @@
namespace MyGame.Example
{
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public enum AnyUniqueAliases : byte
{
NONE = 0,
@@ -37,5 +38,48 @@ public class AnyUniqueAliasesUnion {
}
}
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;
}
}
}

View File

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

View File

@@ -48,11 +48,19 @@ public struct ArrayTable : IFlatbufferObject
public class ArrayTableT
{
[Newtonsoft.Json.JsonProperty("a")]
public MyGame.Example.ArrayStructT A { get; set; }
public ArrayTableT() {
this.A = new MyGame.Example.ArrayStructT();
}
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);
}
}

View File

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

View File

@@ -593,50 +593,142 @@ public struct Monster : IFlatbufferObject
public class MonsterT
{
[Newtonsoft.Json.JsonProperty("pos")]
public MyGame.Example.Vec3T Pos { get; set; }
[Newtonsoft.Json.JsonProperty("mana")]
public short Mana { get; set; }
[Newtonsoft.Json.JsonProperty("hp")]
public short Hp { get; set; }
[Newtonsoft.Json.JsonProperty("name")]
public string Name { get; set; }
[Newtonsoft.Json.JsonProperty("inventory")]
public List<byte> Inventory { get; set; }
[Newtonsoft.Json.JsonProperty("color")]
public MyGame.Example.Color Color { get; set; }
[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))]
public MyGame.Example.AnyUnion Test { get; set; }
[Newtonsoft.Json.JsonProperty("test4")]
public List<MyGame.Example.TestT> Test4 { get; set; }
[Newtonsoft.Json.JsonProperty("testarrayofstring")]
public List<string> Testarrayofstring { get; set; }
[Newtonsoft.Json.JsonProperty("testarrayoftables")]
public List<MyGame.Example.MonsterT> Testarrayoftables { get; set; }
[Newtonsoft.Json.JsonProperty("enemy")]
public MyGame.Example.MonsterT Enemy { get; set; }
[Newtonsoft.Json.JsonProperty("testnestedflatbuffer")]
public List<byte> Testnestedflatbuffer { get; set; }
[Newtonsoft.Json.JsonProperty("testempty")]
public MyGame.Example.StatT Testempty { get; set; }
[Newtonsoft.Json.JsonProperty("testbool")]
public bool Testbool { get; set; }
[Newtonsoft.Json.JsonProperty("testhashs32_fnv1")]
[Newtonsoft.Json.JsonIgnore()]
public int Testhashs32Fnv1 { get; set; }
[Newtonsoft.Json.JsonProperty("testhashu32_fnv1")]
[Newtonsoft.Json.JsonIgnore()]
public uint Testhashu32Fnv1 { get; set; }
[Newtonsoft.Json.JsonProperty("testhashs64_fnv1")]
[Newtonsoft.Json.JsonIgnore()]
public long Testhashs64Fnv1 { get; set; }
[Newtonsoft.Json.JsonProperty("testhashu64_fnv1")]
[Newtonsoft.Json.JsonIgnore()]
public ulong Testhashu64Fnv1 { get; set; }
[Newtonsoft.Json.JsonProperty("testhashs32_fnv1a")]
[Newtonsoft.Json.JsonIgnore()]
public int Testhashs32Fnv1a { get; set; }
[Newtonsoft.Json.JsonProperty("testhashu32_fnv1a")]
[Newtonsoft.Json.JsonIgnore()]
public uint Testhashu32Fnv1a { get; set; }
[Newtonsoft.Json.JsonProperty("testhashs64_fnv1a")]
[Newtonsoft.Json.JsonIgnore()]
public long Testhashs64Fnv1a { get; set; }
[Newtonsoft.Json.JsonProperty("testhashu64_fnv1a")]
[Newtonsoft.Json.JsonIgnore()]
public ulong Testhashu64Fnv1a { get; set; }
[Newtonsoft.Json.JsonProperty("testarrayofbools")]
public List<bool> Testarrayofbools { get; set; }
[Newtonsoft.Json.JsonProperty("testf")]
public float Testf { get; set; }
[Newtonsoft.Json.JsonProperty("testf2")]
public float Testf2 { get; set; }
[Newtonsoft.Json.JsonProperty("testf3")]
public float Testf3 { get; set; }
[Newtonsoft.Json.JsonProperty("testarrayofstring2")]
public List<string> Testarrayofstring2 { get; set; }
[Newtonsoft.Json.JsonProperty("testarrayofsortedstruct")]
public List<MyGame.Example.AbilityT> Testarrayofsortedstruct { get; set; }
[Newtonsoft.Json.JsonProperty("flex")]
public List<byte> Flex { get; set; }
[Newtonsoft.Json.JsonProperty("test5")]
public List<MyGame.Example.TestT> Test5 { get; set; }
[Newtonsoft.Json.JsonProperty("vector_of_longs")]
public List<long> VectorOfLongs { get; set; }
[Newtonsoft.Json.JsonProperty("vector_of_doubles")]
public List<double> VectorOfDoubles { get; set; }
[Newtonsoft.Json.JsonProperty("parent_namespace_test")]
public MyGame.InParentNamespaceT ParentNamespaceTest { get; set; }
[Newtonsoft.Json.JsonProperty("vector_of_referrables")]
public List<MyGame.Example.ReferrableT> VectorOfReferrables { get; set; }
[Newtonsoft.Json.JsonProperty("single_weak_reference")]
[Newtonsoft.Json.JsonIgnore()]
public ulong SingleWeakReference { get; set; }
[Newtonsoft.Json.JsonProperty("vector_of_weak_references")]
[Newtonsoft.Json.JsonIgnore()]
public List<ulong> VectorOfWeakReferences { get; set; }
[Newtonsoft.Json.JsonProperty("vector_of_strong_referrables")]
public List<MyGame.Example.ReferrableT> VectorOfStrongReferrables { get; set; }
[Newtonsoft.Json.JsonProperty("co_owning_reference")]
[Newtonsoft.Json.JsonIgnore()]
public ulong CoOwningReference { get; set; }
[Newtonsoft.Json.JsonProperty("vector_of_co_owning_references")]
[Newtonsoft.Json.JsonIgnore()]
public List<ulong> VectorOfCoOwningReferences { get; set; }
[Newtonsoft.Json.JsonProperty("non_owning_reference")]
[Newtonsoft.Json.JsonIgnore()]
public ulong NonOwningReference { get; set; }
[Newtonsoft.Json.JsonProperty("vector_of_non_owning_references")]
[Newtonsoft.Json.JsonIgnore()]
public List<ulong> VectorOfNonOwningReferences { get; set; }
[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))]
public MyGame.Example.AnyUniqueAliasesUnion AnyUnique { get; set; }
[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))]
public MyGame.Example.AnyAmbiguousAliasesUnion AnyAmbiguous { get; set; }
[Newtonsoft.Json.JsonProperty("vector_of_enums")]
public List<MyGame.Example.Color> VectorOfEnums { get; set; }
[Newtonsoft.Json.JsonProperty("signed_enum")]
public MyGame.Example.Race SignedEnum { get; set; }
public MonsterT() {
@@ -686,6 +778,13 @@ public class MonsterT
this.VectorOfEnums = null;
this.SignedEnum = MyGame.Example.Race.None;
}
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);
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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