public static string Generate(JsonLazyLoader lazyLoader) { return($@" using System; using System.Buffers; using System.Text.Json; namespace {lazyLoader.Namespace} {{ {lazyLoader.Accesibility} readonly ref partial struct {lazyLoader.TypeName} {{ private readonly Utf8JsonReader _jsonReader; public readonly bool HasValue {{ get; }} public {lazyLoader.TypeName}(ref Utf8JsonReader jsonReader) {{ HasValue = true; _jsonReader = jsonReader; jsonReader.Skip(); }} public {lazyLoader.LazyType.FullName} Load() {{ var jsonReader = _jsonReader; return {DeserializationGenerator.Generate("jsonReader", lazyLoader.LazyType)}; }} }} }} "); }
private static string GenerateFieldDeserializer(JsonField field) { return($@" if ({GenerateFieldNameCondition(field)}) {{ jsonReader.Read(); {field.Name} = {DeserializationGenerator.Generate("jsonReader", field.Type)}; }}"); }
public static string Generate(JsonDictionary dictionary) { return($@" using System; using System.Buffers; using System.Text.Json; namespace {dictionary.Namespace} {{ {dictionary.Accesibility} readonly ref partial struct {dictionary.TypeName} {{ private readonly Utf8JsonReader _jsonReader; public readonly bool HasValue {{ get; }} public {dictionary.TypeName}(ReadOnlySpan<byte> jsonData) : this(new Utf8JsonReader(jsonData, new JsonReaderOptions {{ CommentHandling = JsonCommentHandling.Skip }})) {{}} public {dictionary.TypeName}(ReadOnlySequence<byte> jsonData) : this(new Utf8JsonReader(jsonData, new JsonReaderOptions {{ CommentHandling = JsonCommentHandling.Skip }})) {{}} private {dictionary.TypeName}(Utf8JsonReader jsonReader) : this(ref jsonReader) {{}} public {dictionary.TypeName}(ref Utf8JsonReader jsonReader) {{ if (jsonReader.TokenType != JsonTokenType.StartObject && jsonReader.TokenType != JsonTokenType.Null) jsonReader.Read(); switch (jsonReader.TokenType) {{ case JsonTokenType.StartObject: HasValue = true; _jsonReader = jsonReader; _jsonReader.Read(); jsonReader.Skip(); break; case JsonTokenType.Null: HasValue = false; _jsonReader = default; break; default: throw new JsonException($""Expected '{{{{', but got {{jsonReader.TokenType}}""); }} }} public bool Any() => HasValue && _jsonReader.TokenType != JsonTokenType.EndObject; public Enumerator GetEnumerator() => new Enumerator(_jsonReader); public ref struct Enumerator {{ private Utf8JsonReader _jsonReader; public Enumerator(in Utf8JsonReader jsonReader) {{ _jsonReader = jsonReader; Current = default; }} public KeyValuePair Current {{ get; private set; }} public bool MoveNext() {{ if (_jsonReader.TokenType == JsonTokenType.EndObject || _jsonReader.TokenType == JsonTokenType.None) return false; var key = {DeserializationGenerator.Generate("_jsonReader", dictionary.KeyType)}; _jsonReader.Read(); var value = {DeserializationGenerator.Generate("_jsonReader", dictionary.ValueType)}; _jsonReader.Read(); Current = new KeyValuePair(key, value); return true; }} public readonly ref struct KeyValuePair {{ public {dictionary.KeyType} Key {{ get; }} public {dictionary.ValueType} Value {{ get; }} public KeyValuePair({dictionary.KeyType} key, {dictionary.ValueType} value) {{ Key = key; Value = value; }} }} }} }} }} "); }
public static string Generate(JsonArray array) { return($@" using System; using System.Buffers; using System.Text.Json; namespace {array.Namespace} {{ {array.Accesibility} readonly ref partial struct {array.TypeName} {{ private readonly Utf8JsonReader _jsonReader; public readonly bool HasValue {{ get; }} public {array.TypeName}(ReadOnlySpan<byte> jsonData) : this(new Utf8JsonReader(jsonData, new JsonReaderOptions {{ CommentHandling = JsonCommentHandling.Skip }})) {{}} public {array.TypeName}(ReadOnlySequence<byte> jsonData) : this(new Utf8JsonReader(jsonData, new JsonReaderOptions {{ CommentHandling = JsonCommentHandling.Skip }})) {{}} private {array.TypeName}(Utf8JsonReader jsonReader) : this(ref jsonReader) {{}} public {array.TypeName}(ref Utf8JsonReader jsonReader) {{ if (jsonReader.TokenType != JsonTokenType.StartArray && jsonReader.TokenType != JsonTokenType.Null) jsonReader.Read(); switch (jsonReader.TokenType) {{ case JsonTokenType.StartArray: HasValue = true; _jsonReader = jsonReader; _jsonReader.Read(); jsonReader.Skip(); break; case JsonTokenType.Null: HasValue = false; _jsonReader = default; break; default: throw new JsonException($""Expected '[', but got {{jsonReader.TokenType}}""); }} }} public bool Any() => HasValue && _jsonReader.TokenType != JsonTokenType.EndArray; public Enumerator GetEnumerator() => new Enumerator(_jsonReader); public ref struct Enumerator {{ private Utf8JsonReader _jsonReader; public Enumerator(in Utf8JsonReader jsonReader) {{ _jsonReader = jsonReader; Current = default; }} public {array.ElementType} Current {{ get; private set; }} public bool MoveNext() {{ if (_jsonReader.TokenType == JsonTokenType.EndArray || _jsonReader.TokenType == JsonTokenType.None) return false; Current = {DeserializationGenerator.Generate("_jsonReader", array.ElementType)}; _jsonReader.Read(); return true; }} }} }} }} "); }