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

@@ -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() {