private string GenerateReadBoolean(JensonPropertyInfo p) { var w = new SourceWriter(); w.Line($"if (reader.TokenType == JsonTokenType.True) {p.Name} = true;"); w.Line($"else if (reader.TokenType == JsonTokenType.False) {p.Name} = false;"); w.Line("else throw new JsonException(\"Boolean property must have true or false value.\");"); return(w.ToString()); }
private static JensonPropertyInfo MergePropertyInfo(JensonPropertyInfo baseProp, JensonPropertyInfo overwrite) => new JensonPropertyInfo( baseProp.Name, overwrite.JsonName ?? baseProp.Name, overwrite.TypeName, overwrite.IsReadOnly, overwrite.ShouldSerialize && baseProp.ShouldSerialize, // TODO handle JsonInclude overwrite.ShouldDeserialize && baseProp.ShouldDeserialize, baseProp.CanBeNull, overwrite.Order ?? baseProp.Order, overwrite.ShouldSerializeFunction ?? baseProp.ShouldSerializeFunction);
private string GenerateReadArray(JensonPropertyInfo p) { var w = new SourceWriter(); w.Line("if (reader.TokenType != JsonTokenType.StartArray) throw new JsonException();"); w.Line(); w.Line($"{p.Name} = JsonSerializer.Deserialize<{p.TypeName}>(ref reader);"); return(w.ToString()); w.Line("var restore = reader;"); w.Line("var length = 0;"); w.Line(); w.Line("while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)"); w.Line("{"); w.Indent(); w.Line("if (reader.TokenType != JsonTokenType.Comment) length++;"); w.Line("reader.Skip();"); w.Dedent(); w.Line("}"); w.Line(); w.Line("reader = restore;"); w.Line($"{p.Name} = new {p.TypeName.Substring(0, p.TypeName.Length - 2)}[length];"); var itemType = p.ArrayItemType; if (itemType.EndsWith("[]")) { throw new NotImplementedException("Nested arrays are not implemented."); } //var attribList = new List<OpAttribute>(); //while (reader.Read()) //{ // if (reader.TokenType == JsonTokenType.EndArray) // { // attributes = attribList.ToArray(); // break; // } return(w.ToString()); }
private string GenerateWriteArray(JensonPropertyInfo p) { return($"JsonSerializer.Serialize(writer, value.{p.Name}, options);"); //var w = new SourceWriter(); //w.Line("writer.WriteStartArray();"); //w.Line(); //w.Line($"foreach (var item in value.{p.Name})"); //w.Line("{"); //w.Indent(); //var writeValueString = p switch //{ // _ when p.IsArray => "throw new NotImplementedException(\"array\");", // _ when p.IsString || // p.IsBoolean || // p.IsDateTime || // p.IsDateTimeOffset || // p.IsGuid => $"writer.WriteString(_{p.Name}Name, value.{p.Name});", // _ when p.IsByte || // p.IsDecimal || // p.IsDouble || // p.IsInt16 || // p.IsInt32 || // p.IsInt64 || // p.IsSByte || // p.IsSingle || // p.IsUInt16 || // p.IsUInt32 || // p.IsUInt64 => $"writer.WriteNumber(_{p.Name}Name, value.{p.Name});", // _ => $"JsonSerializer.Serialize(writer, value.{p.Name}, options);" //}; //w.Dedent(); //w.Line("}"); //w.Line(); //w.Line("writer.WriteEndArray();"); //return w.ToString(); }
private void WriteTypeDiscriminatorConverterBody(SourceWriter w, string baseType, JensonPropertyInfo p, string discrFun) { w.Line($"public override {baseType} Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)"); w.Line("{"); w.Indent(); w.Line("var discrReader = reader;"); w.Line($"var discriminatorName = Encoding.UTF8.GetBytes(\"{p.Name}\");"); w.Line(); w.Line("if (discrReader.TokenType != JsonTokenType.StartObject) throw new JsonException(\"Expected start of object.\");"); w.Line(); w.Line("while (discrReader.Read())"); w.Line("{"); w.Indent(); w.Line("if (discrReader.ValueTextEquals(discriminatorName))"); w.Line("{"); w.Indent(); w.Line("if (!discrReader.Read()) throw new JsonException();"); // TODO proper deserialization of value w.Line($"var value = JsonSerializer.Deserialize<{p.TypeName}>(ref discrReader);"); w.Line($"var realType = {baseType}.{discrFun}(value);"); w.Line($"return ({baseType}) JsonSerializer.Deserialize(ref reader, realType, options);"); w.Dedent(); w.Line("}"); w.Line(); w.Line("discrReader.Skip();"); w.Dedent(); w.Line("}"); w.Line(); w.Line("throw new JsonException(\"Missing type discriminator.\");"); w.Dedent(); w.Line("}"); w.Line(); w.Line($"public override void Write(Utf8JsonWriter writer, {baseType} value, JsonSerializerOptions options)"); w.Line("{"); w.Indent(); w.Line("throw new NotImplementedException(\"A converter for subclasses must always be used for writing.\");"); w.Dedent(); w.Line("}"); }