@@ -147,6 +147,21 @@ class CSharpGenerator : public BaseGenerator {
147147 std::string one_file_code;
148148 cur_name_space_ = parser_.current_namespace_ ;
149149
150+ if (parser_.opts .cs_gen_json_serializer &&
151+ parser_.opts .generate_object_based_api ) {
152+ std::string contractresolvercode;
153+ GenJsonContractResolver (&contractresolvercode);
154+
155+ if (parser_.opts .one_file ) {
156+ one_file_code += contractresolvercode;
157+ } else {
158+ if (!SaveType (" JsonContractResolver" , *parser_.current_namespace_ ,
159+ contractresolvercode, true , parser_.opts )) {
160+ return false ;
161+ }
162+ }
163+ }
164+
150165 for (auto it = parser_.enums_ .vec .begin (); it != parser_.enums_ .vec .end ();
151166 ++it) {
152167 std::string enumcode;
@@ -2395,6 +2410,9 @@ class CSharpGenerator : public BaseGenerator {
23952410 auto utype_name = NamespacedName (*field.value .type .enum_def );
23962411 code +=
23972412 " [Newtonsoft.Json.JsonProperty(\" " + field.name + " _type\" )]\n " ;
2413+ if (field.deprecated == FieldDef::kDeprecatedReadOnly ) {
2414+ code += " [JsonReadOnly()]\n " ;
2415+ }
23982416 if (IsVector (field.value .type )) {
23992417 code += " private " + utype_name + " [] " + camel_name + " Type {\n " ;
24002418 code += " get {\n " ;
@@ -2442,6 +2460,8 @@ class CSharpGenerator : public BaseGenerator {
24422460 }
24432461 if (field.attributes .Lookup (" hash" )) {
24442462 code += " [Newtonsoft.Json.JsonIgnore()]\n " ;
2463+ } else if (field.deprecated == FieldDef::kDeprecatedReadOnly ) {
2464+ code += " [JsonReadOnly()]\n " ;
24452465 }
24462466 }
24472467 code += " public " + type_name + " " + camel_name + " { get; set; }\n " ;
@@ -2491,12 +2511,18 @@ class CSharpGenerator : public BaseGenerator {
24912511 code += " public static " + class_name +
24922512 " DeserializeFromJson(string jsonText) {\n " ;
24932513 code += " return Newtonsoft.Json.JsonConvert.DeserializeObject<" +
2494- class_name + " >(jsonText);\n " ;
2514+ class_name +
2515+ " >(jsonText, new Newtonsoft.Json.JsonSerializerSettings() {\n " ;
2516+ code += " ContractResolver = new JsonContractResolver(),\n " ;
2517+ code += " });\n " ;
24952518 code += " }\n " ;
24962519 code += " public string SerializeToJson() {\n " ;
24972520 code +=
2498- " return Newtonsoft.Json.JsonConvert.SerializeObject(this, "
2499- " Newtonsoft.Json.Formatting.Indented);\n " ;
2521+ " return Newtonsoft.Json.JsonConvert.SerializeObject(this, new "
2522+ " Newtonsoft.Json.JsonSerializerSettings() {\n " ;
2523+ code += " ContractResolver = new JsonContractResolver(),\n " ;
2524+ code += " Formatting = Newtonsoft.Json.Formatting.Indented,\n " ;
2525+ code += " });\n " ;
25002526 code += " }\n " ;
25012527 }
25022528 if (parser_.root_struct_def_ == &struct_def) {
@@ -2515,6 +2541,35 @@ class CSharpGenerator : public BaseGenerator {
25152541 code += " }\n\n " ;
25162542 }
25172543
2544+ void GenJsonContractResolver (std::string *code_ptr) const {
2545+ auto &code = *code_ptr;
2546+ code +=
2547+ " [AttributeUsage(AttributeTargets.Property | "
2548+ " AttributeTargets.Field)]\n " ;
2549+ code += " class JsonReadOnlyAttribute : Attribute {\n " ;
2550+ code += " }\n\n " ;
2551+
2552+ code +=
2553+ " public class JsonContractResolver : "
2554+ " Newtonsoft.Json.Serialization.DefaultContractResolver\n " ;
2555+ code += " {\n " ;
2556+ code +=
2557+ " protected override Newtonsoft.Json.Serialization.JsonProperty "
2558+ " CreateProperty(System.Reflection.MemberInfo member, "
2559+ " Newtonsoft.Json.MemberSerialization memberSerialization)\n " ;
2560+ code += " {\n " ;
2561+ code +=
2562+ " var property = base.CreateProperty(member, "
2563+ " memberSerialization);\n " ;
2564+ code +=
2565+ " if (Attribute.IsDefined(member, "
2566+ " typeof(JsonReadOnlyAttribute)))\n " ;
2567+ code += " property.Readable = false;\n " ;
2568+ code += " return property;\n " ;
2569+ code += " }\n " ;
2570+ code += " }\n\n " ;
2571+ }
2572+
25182573 // This tracks the current namespace used to determine if a type need to be
25192574 // prefixed by its namespace
25202575 const Namespace *cur_name_space_;
0 commit comments