mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-23 18:51:47 +00:00
[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:
@@ -50,6 +50,7 @@ public struct Attacker : IFlatbufferObject
|
||||
|
||||
public class AttackerT
|
||||
{
|
||||
[Newtonsoft.Json.JsonProperty("sword_attack_damage")]
|
||||
public int SwordAttackDamage { get; set; }
|
||||
|
||||
public AttackerT() {
|
||||
|
||||
@@ -39,6 +39,7 @@ public struct BookReader : IFlatbufferObject
|
||||
|
||||
public class BookReaderT
|
||||
{
|
||||
[Newtonsoft.Json.JsonProperty("books_read")]
|
||||
public int BooksRead { get; set; }
|
||||
|
||||
public BookReaderT() {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// </auto-generated>
|
||||
|
||||
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
|
||||
public enum Character : byte
|
||||
{
|
||||
NONE = 0,
|
||||
@@ -43,3 +44,49 @@ public class CharacterUnion {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -146,12 +146,50 @@ public struct Movie : IFlatbufferObject
|
||||
|
||||
public class MovieT
|
||||
{
|
||||
[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))]
|
||||
public CharacterUnion MainCharacter { get; set; }
|
||||
[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))]
|
||||
public List<CharacterUnion> Characters { get; set; }
|
||||
|
||||
public MovieT() {
|
||||
this.MainCharacter = null;
|
||||
this.Characters = null;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@ public struct Rapunzel : IFlatbufferObject
|
||||
|
||||
public class RapunzelT
|
||||
{
|
||||
[Newtonsoft.Json.JsonProperty("hair_length")]
|
||||
public int HairLength { get; set; }
|
||||
|
||||
public RapunzelT() {
|
||||
|
||||
Reference in New Issue
Block a user