Esempio n. 1
0
 /// <summary>
 /// Generates code for writing one field as a JSON string
 /// </summary>
 public static void FieldJsonWriter(ProtoMessage m, Field f, CodeWriter cw, Options options)
 {
     cw.WriteLine("writer.Write(\"\\\"" + f.ProtoName + "\\\":\");");
     if (f.Rule == FieldRule.Repeated)
     {
         cw.WriteLine();
         cw.IfBracket("instance." + f.CsName + " != null");
         cw.WriteLine("writer.Write(\"[\");");
         cw.WriteLine("var first = true;");
         cw.ForeachBracket("var i" + f.ID + " in instance." + f.CsName);
         cw.WriteLine("if (!first) writer.Write(\",\");");
         cw.WriteLine("else first = false;");
         cw.WriteLine(FieldWriterJsonType(f, m.RequiredMessageTables, "writer", "i" + f.ID));
         cw.EndBracket();
         cw.WriteLine("writer.Write(\"]\");");
         cw.ElseBracket();
         cw.WriteLine("writer.Write(\"null\");");
         cw.EndBracket();
         return;
     }
     else if (f.Rule == FieldRule.Optional)
     {
         if (options.Nullable ||
             f.ProtoType is ProtoMessage ||
             f.ProtoType.ProtoName == ProtoBuiltin.String ||
             f.ProtoType.ProtoName == ProtoBuiltin.Bytes)
         {
             var nullable = (f.ProtoType.Nullable || options.Nullable) && !(f.ProtoType.OptionType == "interface" && m.RequiredMessageTables.Contains(f.ProtoType));
             if (nullable) //Struct always exist, not optional
             {
                 cw.IfBracket("instance." + f.CsName + " != null");
             }
             var needValue = !f.ProtoType.Nullable && options.Nullable;
             cw.WriteLine(FieldWriterJsonType(f, m.RequiredMessageTables, "writer", "instance." + f.CsName + (needValue ? ".Value" : "")));
             if (nullable)   //Struct always exist, not optional
             {
                 cw.ElseBracket();
                 cw.WriteLine("writer.Write(\"null\");");
                 cw.EndBracket();
             }
             return;
         }
         if (f.ProtoType is ProtoEnum)
         {
             cw.WriteLine(FieldWriterJsonType(f, m.RequiredMessageTables, "writer", "instance." + f.CsName));
             return;
         }
         cw.WriteLine(FieldWriterJsonType(f, m.RequiredMessageTables, "writer", "instance." + f.CsName));
         return;
     }
     else if (f.Rule == FieldRule.Required)
     {
         if (f.ProtoType is ProtoMessage && f.ProtoType.OptionType != "struct" ||
             f.ProtoType.ProtoName == ProtoBuiltin.String ||
             f.ProtoType.ProtoName == ProtoBuiltin.Bytes)
         {
             cw.WriteLine("if (instance." + f.CsName + " == null)");
             cw.WriteIndent("throw new ArgumentNullException(\"" + f.CsName + "\", \"Required by proto specification.\");");
         }
         cw.WriteLine(FieldWriterJsonType(f, m.RequiredMessageTables, "writer", "instance." + f.CsName));
         return;
     }
     throw new NotImplementedException("Unknown rule: " + f.Rule);
 }