public override async Task WriteObjectAsync(JsonTextWriter jsonWriter, ITraktCastMember obj, CancellationToken cancellationToken = default) { CheckJsonTextWriter(jsonWriter); await jsonWriter.WriteStartObjectAsync(cancellationToken).ConfigureAwait(false); if (obj.Characters != null) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_CHARACTERS, cancellationToken).ConfigureAwait(false); await JsonWriterHelper.WriteStringArrayAsync(jsonWriter, obj.Characters, cancellationToken).ConfigureAwait(false); } if (obj.Person != null) { var personObjectJsonWriter = new PersonObjectJsonWriter(); await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_PERSON, cancellationToken).ConfigureAwait(false); await personObjectJsonWriter.WriteObjectAsync(jsonWriter, obj.Person, cancellationToken).ConfigureAwait(false); } await jsonWriter.WriteEndObjectAsync(cancellationToken).ConfigureAwait(false); }
public override async Task WriteObjectAsync(JsonTextWriter jsonWriter, ITraktPersonMovieCreditsCrewItem obj, CancellationToken cancellationToken = default) { CheckJsonTextWriter(jsonWriter); await jsonWriter.WriteStartObjectAsync(cancellationToken).ConfigureAwait(false); if (obj.Jobs != null) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_JOBS, cancellationToken).ConfigureAwait(false); await JsonWriterHelper.WriteStringArrayAsync(jsonWriter, obj.Jobs, cancellationToken).ConfigureAwait(false); } if (obj.Movie != null) { var movieObjectJsonWriter = new MovieObjectJsonWriter(); await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_MOVIE, cancellationToken).ConfigureAwait(false); await movieObjectJsonWriter.WriteObjectAsync(jsonWriter, obj.Movie, cancellationToken).ConfigureAwait(false); } await jsonWriter.WriteEndObjectAsync(cancellationToken).ConfigureAwait(false); }
private void WriteNumberValueIndented(ulong value) { int idx = 0; if (_currentDepth < 0) { while (_buffer.Length <= idx) { GrowAndEnsure(); } _buffer[idx++] = JsonConstants.ListSeperator; } if (_tokenType != JsonTokenType.None) { WriteNewLine(ref idx); } int indent = Indentation; while (true) { bool result = JsonWriterHelper.TryWriteIndentation(_buffer.Slice(idx), indent, out int bytesWritten); idx += bytesWritten; if (result) { break; } indent -= bytesWritten; AdvanceAndGrow(idx); idx = 0; } WriteNumberValueFormatLoop(value, ref idx); Advance(idx); }
public override async Task WriteObjectAsync(JsonTextWriter jsonWriter, ITraktPersonShowCreditsCastItem obj, CancellationToken cancellationToken = default) { CheckJsonTextWriter(jsonWriter); await jsonWriter.WriteStartObjectAsync(cancellationToken).ConfigureAwait(false); if (obj.Characters != null) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_CHARACTERS, cancellationToken).ConfigureAwait(false); await JsonWriterHelper.WriteStringArrayAsync(jsonWriter, obj.Characters, cancellationToken).ConfigureAwait(false); } if (obj.EpisodeCount.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_EPISODE_COUNT, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.EpisodeCount.Value, cancellationToken).ConfigureAwait(false); } if (obj.SeriesRegular.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_SERIES_REGULAR, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.SeriesRegular.Value, cancellationToken).ConfigureAwait(false); } if (obj.Show != null) { var movieObjectJsonWriter = new ShowObjectJsonWriter(); await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_SHOW, cancellationToken).ConfigureAwait(false); await movieObjectJsonWriter.WriteObjectAsync(jsonWriter, obj.Show, cancellationToken).ConfigureAwait(false); } await jsonWriter.WriteEndObjectAsync(cancellationToken).ConfigureAwait(false); }
public override async Task WriteObjectAsync(JsonTextWriter jsonWriter, ITraktEpisode obj, CancellationToken cancellationToken = default) { CheckJsonTextWriter(jsonWriter); await jsonWriter.WriteStartObjectAsync(cancellationToken).ConfigureAwait(false); if (obj.SeasonNumber.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_SEASON, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.SeasonNumber, cancellationToken).ConfigureAwait(false); } if (obj.Number.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_NUMBER, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Number, cancellationToken).ConfigureAwait(false); } if (!string.IsNullOrEmpty(obj.Title)) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_TITLE, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Title, cancellationToken).ConfigureAwait(false); } if (obj.Ids != null) { var episodeIdsObjectJsonWriter = new EpisodeIdsObjectJsonWriter(); await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_IDS, cancellationToken).ConfigureAwait(false); await episodeIdsObjectJsonWriter.WriteObjectAsync(jsonWriter, obj.Ids, cancellationToken).ConfigureAwait(false); } if (obj.NumberAbsolute.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_NUMBER_ABSOLUTE, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.NumberAbsolute, cancellationToken).ConfigureAwait(false); } if (!string.IsNullOrEmpty(obj.Overview)) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_OVERVIEW, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Overview, cancellationToken).ConfigureAwait(false); } if (obj.Runtime.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_RUNTIME, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Runtime, cancellationToken).ConfigureAwait(false); } if (obj.Rating.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_RATING, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Rating, cancellationToken).ConfigureAwait(false); } if (obj.Votes.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_VOTES, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Votes, cancellationToken).ConfigureAwait(false); } if (obj.FirstAired.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_FIRST_AIRED, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.FirstAired.Value.ToTraktLongDateTimeString(), cancellationToken).ConfigureAwait(false); } if (obj.UpdatedAt.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_UPDATED_AT, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.UpdatedAt.Value.ToTraktLongDateTimeString(), cancellationToken).ConfigureAwait(false); } if (obj.AvailableTranslationLanguageCodes != null) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_AVAILABLE_TRANSLATIONS, cancellationToken).ConfigureAwait(false); await JsonWriterHelper.WriteStringArrayAsync(jsonWriter, obj.AvailableTranslationLanguageCodes, cancellationToken).ConfigureAwait(false); } if (obj.Translations != null) { var episodeTranslationArrayJsonWriter = new ArrayJsonWriter <ITraktEpisodeTranslation>(); await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_TRANSLATIONS, cancellationToken).ConfigureAwait(false); await episodeTranslationArrayJsonWriter.WriteArrayAsync(jsonWriter, obj.Translations, cancellationToken).ConfigureAwait(false); } if (obj.CommentCount.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_COMMENT_COUNT, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.CommentCount, cancellationToken).ConfigureAwait(false); } await jsonWriter.WriteEndObjectAsync(cancellationToken).ConfigureAwait(false); }
private void DeterminePropertyName(JsonSerializerOptions options) { if (PropertyInfo == null) { return; } JsonPropertyNameAttribute nameAttribute = GetAttribute <JsonPropertyNameAttribute>(PropertyInfo); if (nameAttribute != null) { string name = nameAttribute.Name; if (name == null) { ThrowHelper.ThrowInvalidOperationException_SerializerPropertyNameNull(ParentClassType, this); } NameAsString = name; } else if (options.PropertyNamingPolicy != null) { string name = options.PropertyNamingPolicy.ConvertName(PropertyInfo.Name); if (name == null) { ThrowHelper.ThrowInvalidOperationException_SerializerPropertyNameNull(ParentClassType, this); } NameAsString = name; } else { NameAsString = PropertyInfo.Name; } Debug.Assert(NameAsString != null); // At this point propertyName is valid UTF16, so just call the simple UTF16->UTF8 encoder. Name = Encoding.UTF8.GetBytes(NameAsString); // Set the compare name. if (options.PropertyNameCaseInsensitive) { NameUsedToCompareAsString = NameAsString.ToUpperInvariant(); NameUsedToCompare = Encoding.UTF8.GetBytes(NameUsedToCompareAsString); } else { NameUsedToCompareAsString = NameAsString; NameUsedToCompare = Name; } // Cache the escaped name. #if true // temporary behavior until the writer can accept escaped string. EscapedName = Name; #else int valueIdx = JsonWriterHelper.NeedsEscaping(_name); if (valueIdx == -1) { _escapedName = _name; } else { byte[] pooledName = null; int length = JsonWriterHelper.GetMaxEscapedLength(_name.Length, valueIdx); Span <byte> escapedName = length <= JsonConstants.StackallocThreshold ? stackalloc byte[length] : (pooledName = ArrayPool <byte> .Shared.Rent(length)); JsonWriterHelper.EscapeString(_name, escapedName, 0, out int written); _escapedName = escapedName.Slice(0, written).ToArray(); if (pooledName != null) { // We clear the array because it is "user data" (although a property name). new Span <byte>(pooledName, 0, written).Clear(); ArrayPool <byte> .Shared.Return(pooledName); } } #endif }
private void AddProperty(Type propertyType, PropertyInfo propertyInfo, Type classType, JsonSerializerOptions options) { Type collectionElementType = null; ClassType propertyClassType = GetClassType(propertyType); if (propertyClassType == ClassType.Enumerable) { collectionElementType = GetElementType(propertyType); // todo: if collectionElementType is object, create loosely-typed collection (JsonArray). } // Create the JsonPropertyInfo<TType, TProperty> Type genericPropertyType; if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { Type underlyingPropertyType = Nullable.GetUnderlyingType(propertyType); genericPropertyType = typeof(JsonPropertyInfoNullable <,>).MakeGenericType(classType, underlyingPropertyType); } else { genericPropertyType = typeof(JsonPropertyInfoNotNullable <,>).MakeGenericType(classType, propertyType); } JsonPropertyInfo jsonInfo = (JsonPropertyInfo)Activator.CreateInstance( genericPropertyType, BindingFlags.Instance | BindingFlags.NonPublic, binder: null, new object[] { classType, propertyType, propertyInfo, collectionElementType, options }, culture: null); if (propertyInfo != null) { string propertyName = propertyInfo.Name; // At this point propertyName is valid UTF16, so just call the simple UTF16->UTF8 encoder. byte[] propertyNameBytes = Encoding.UTF8.GetBytes(propertyName); jsonInfo._name = propertyNameBytes; // Cache the escaped name. int valueIdx = JsonWriterHelper.NeedsEscaping(propertyNameBytes); if (valueIdx == -1) { jsonInfo._escapedName = propertyNameBytes; } else { int length = JsonWriterHelper.GetMaxEscapedLength(propertyNameBytes.Length, valueIdx); byte[] tempArray = ArrayPool <byte> .Shared.Rent(length); JsonWriterHelper.EscapeString(propertyNameBytes, tempArray, valueIdx, out int written); jsonInfo._escapedName = new byte[written]; tempArray.CopyTo(jsonInfo._escapedName, 0); // We clear the array because it is "user data" (although a property name). ArrayPool <byte> .Shared.Return(tempArray, clearArray : true); } _property_refs.Add(new PropertyRef(GetKey(propertyNameBytes), jsonInfo)); } else { // A single property or an IEnumerable _property_refs.Add(new PropertyRef(0, jsonInfo)); } }
public override async Task WriteObjectAsync(JsonTextWriter jsonWriter, ITraktShow obj, CancellationToken cancellationToken = default) { if (jsonWriter == null) { throw new ArgumentNullException(nameof(jsonWriter)); } await jsonWriter.WriteStartObjectAsync(cancellationToken).ConfigureAwait(false); if (!string.IsNullOrEmpty(obj.Title)) { await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_TITLE, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Title, cancellationToken).ConfigureAwait(false); } if (obj.Year.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_YEAR, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Year, cancellationToken).ConfigureAwait(false); } if (obj.Ids != null) { var showIdsObjectJsonWriter = new ShowIdsObjectJsonWriter(); await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_IDS, cancellationToken).ConfigureAwait(false); await showIdsObjectJsonWriter.WriteObjectAsync(jsonWriter, obj.Ids, cancellationToken).ConfigureAwait(false); } if (!string.IsNullOrEmpty(obj.Overview)) { await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_OVERVIEW, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Overview, cancellationToken).ConfigureAwait(false); } if (obj.FirstAired.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_FIRST_AIRED, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.FirstAired.Value.ToTraktLongDateTimeString(), cancellationToken).ConfigureAwait(false); } if (obj.Airs != null) { var showAirsObjectJsonWriter = new ShowAirsObjectJsonWriter(); await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_AIRS, cancellationToken).ConfigureAwait(false); await showAirsObjectJsonWriter.WriteObjectAsync(jsonWriter, obj.Airs, cancellationToken).ConfigureAwait(false); } if (obj.Runtime.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_RUNTIME, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Runtime, cancellationToken).ConfigureAwait(false); } if (!string.IsNullOrEmpty(obj.Certification)) { await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_CERTIFICATION, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Certification, cancellationToken).ConfigureAwait(false); } if (!string.IsNullOrEmpty(obj.Network)) { await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_NETWORK, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Network, cancellationToken).ConfigureAwait(false); } if (!string.IsNullOrEmpty(obj.CountryCode)) { await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_COUNTRY, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.CountryCode, cancellationToken).ConfigureAwait(false); } if (!string.IsNullOrEmpty(obj.Trailer)) { await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_TRAILER, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Trailer, cancellationToken).ConfigureAwait(false); } if (!string.IsNullOrEmpty(obj.Homepage)) { await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_HOMEPAGE, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Homepage, cancellationToken).ConfigureAwait(false); } if (obj.Status != null) { await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_STATUS, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Status.ObjectName, cancellationToken).ConfigureAwait(false); } if (obj.Rating.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_RATING, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Rating, cancellationToken).ConfigureAwait(false); } if (obj.Votes.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_VOTES, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Votes, cancellationToken).ConfigureAwait(false); } if (obj.UpdatedAt.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_UPDATED_AT, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.UpdatedAt.Value.ToTraktLongDateTimeString(), cancellationToken).ConfigureAwait(false); } if (!string.IsNullOrEmpty(obj.LanguageCode)) { await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_LANGUAGE, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.LanguageCode, cancellationToken).ConfigureAwait(false); } if (obj.AvailableTranslationLanguageCodes != null) { await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_AVAILABLE_TRANSLATIONS, cancellationToken).ConfigureAwait(false); await JsonWriterHelper.WriteStringArrayAsync(jsonWriter, obj.AvailableTranslationLanguageCodes, cancellationToken).ConfigureAwait(false); } if (obj.Genres != null) { await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_GENRES, cancellationToken).ConfigureAwait(false); await JsonWriterHelper.WriteStringArrayAsync(jsonWriter, obj.Genres, cancellationToken).ConfigureAwait(false); } if (obj.AiredEpisodes.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_AIRED_EPISODES, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.AiredEpisodes, cancellationToken).ConfigureAwait(false); } if (obj.Seasons != null) { var seasonArrayJsonWriter = new ArrayJsonWriter <ITraktSeason>(); await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_SEASONS, cancellationToken).ConfigureAwait(false); await seasonArrayJsonWriter.WriteArrayAsync(jsonWriter, obj.Seasons, cancellationToken).ConfigureAwait(false); } if (obj.CommentCount.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.SHOW_PROPERTY_NAME_COMMENT_COUNT, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.CommentCount, cancellationToken).ConfigureAwait(false); } await jsonWriter.WriteEndObjectAsync(cancellationToken).ConfigureAwait(false); }
public override async Task WriteObjectAsync(JsonTextWriter jsonWriter, ITraktMovie obj, CancellationToken cancellationToken = default) { CheckJsonTextWriter(jsonWriter); await jsonWriter.WriteStartObjectAsync(cancellationToken).ConfigureAwait(false); if (!string.IsNullOrEmpty(obj.Title)) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_TITLE, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Title, cancellationToken).ConfigureAwait(false); } if (obj.Year.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_YEAR, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Year, cancellationToken).ConfigureAwait(false); } if (obj.Ids != null) { var movieIdsObjectJsonWriter = new MovieIdsObjectJsonWriter(); await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_IDS, cancellationToken).ConfigureAwait(false); await movieIdsObjectJsonWriter.WriteObjectAsync(jsonWriter, obj.Ids, cancellationToken).ConfigureAwait(false); } if (!string.IsNullOrEmpty(obj.Tagline)) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_TAGLINE, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Tagline, cancellationToken).ConfigureAwait(false); } if (!string.IsNullOrEmpty(obj.Overview)) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_OVERVIEW, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Overview, cancellationToken).ConfigureAwait(false); } if (obj.Released.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_RELEASED, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Released.Value.ToTraktDateString(), cancellationToken).ConfigureAwait(false); } if (obj.Runtime.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_RUNTIME, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Runtime, cancellationToken).ConfigureAwait(false); } if (!string.IsNullOrEmpty(obj.Trailer)) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_TRAILER, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Trailer, cancellationToken).ConfigureAwait(false); } if (!string.IsNullOrEmpty(obj.Homepage)) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_HOMEPAGE, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Homepage, cancellationToken).ConfigureAwait(false); } if (obj.Rating.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_RATING, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Rating, cancellationToken).ConfigureAwait(false); } if (obj.Votes.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_VOTES, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Votes, cancellationToken).ConfigureAwait(false); } if (obj.UpdatedAt.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_UPDATED_AT, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.UpdatedAt.Value.ToTraktLongDateTimeString(), cancellationToken).ConfigureAwait(false); } if (!string.IsNullOrEmpty(obj.LanguageCode)) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_LANGUAGE, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.LanguageCode, cancellationToken).ConfigureAwait(false); } if (obj.AvailableTranslationLanguageCodes != null) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_AVAILABLE_TRANSLATIONS, cancellationToken).ConfigureAwait(false); await JsonWriterHelper.WriteStringArrayAsync(jsonWriter, obj.AvailableTranslationLanguageCodes, cancellationToken).ConfigureAwait(false); } if (obj.Genres != null) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_GENRES, cancellationToken).ConfigureAwait(false); await JsonWriterHelper.WriteStringArrayAsync(jsonWriter, obj.Genres, cancellationToken).ConfigureAwait(false); } if (!string.IsNullOrEmpty(obj.Certification)) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_CERTIFICATION, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Certification, cancellationToken).ConfigureAwait(false); } if (!string.IsNullOrEmpty(obj.CountryCode)) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_COUNTRY, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.CountryCode, cancellationToken).ConfigureAwait(false); } if (obj.CommentCount.HasValue) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_COMMENT_COUNT, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.CommentCount, cancellationToken).ConfigureAwait(false); } if (obj.Status != null) { await jsonWriter.WritePropertyNameAsync(JsonProperties.PROPERTY_NAME_STATUS, cancellationToken).ConfigureAwait(false); await jsonWriter.WriteValueAsync(obj.Status.ObjectName, cancellationToken).ConfigureAwait(false); } await jsonWriter.WriteEndObjectAsync(cancellationToken).ConfigureAwait(false); }
internal static void WriteDictionary <TProperty>( JsonValueConverter <TProperty> converter, JsonSerializerOptions options, ref WriteStackFrame current, Utf8JsonWriter writer) { if (converter == null) { return; } Debug.Assert(current.Enumerator != null); string key; TProperty value; if (current.Enumerator is IEnumerator <KeyValuePair <string, TProperty> > enumerator) { // Avoid boxing for strongly-typed enumerators such as returned from IDictionary<string, TRuntimeProperty> value = enumerator.Current.Value; key = enumerator.Current.Key; } else if (current.Enumerator is IEnumerator <KeyValuePair <string, object> > polymorphicEnumerator) { value = (TProperty)polymorphicEnumerator.Current.Value; key = polymorphicEnumerator.Current.Key; } else { // Todo: support non-generic Dictionary here (IDictionaryEnumerator) throw new NotSupportedException(); } if (value == null) { writer.WriteNull(key); } else { byte[] utf8Key = Encoding.UTF8.GetBytes(key); #if true // temporary behavior until the writer can accept escaped string. converter.Write(utf8Key, value, writer); #else int valueIdx = JsonWriterHelper.NeedsEscaping(utf8Key); if (valueIdx == -1) { converter.Write(utf8Key, value, writer); } else { byte[] pooledKey = null; int length = JsonWriterHelper.GetMaxEscapedLength(utf8Key.Length, valueIdx); Span <byte> escapedKey = length <= JsonConstants.StackallocThreshold ? stackalloc byte[length] : (pooledKey = ArrayPool <byte> .Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8Key, escapedKey, valueIdx, out int written); converter.Write(escapedKey.Slice(0, written), value, writer); if (pooledKey != null) { // We clear the array because it is "user data" (although a property name). new Span <byte>(pooledKey, 0, written).Clear(); ArrayPool <byte> .Shared.Return(pooledKey); } } #endif } }
private void DeterminePropertyName(JsonSerializerOptions options) { if (PropertyInfo != null) { JsonPropertyNameAttribute nameAttribute = GetAttribute <JsonPropertyNameAttribute>(); if (nameAttribute != null) { NameAsString = nameAttribute.Name; // This is detected and thrown by caller. if (NameAsString == null) { return; } } else if (options.PropertyNamingPolicy != null) { NameAsString = options.PropertyNamingPolicy.ConvertName(PropertyInfo.Name); // This is detected and thrown by caller. if (NameAsString == null) { return; } } else { NameAsString = PropertyInfo.Name; } // At this point propertyName is valid UTF16, so just call the simple UTF16->UTF8 encoder. _name = Encoding.UTF8.GetBytes(NameAsString); // Set the compare name. if (options.PropertyNameCaseInsensitive) { CompareNameAsString = NameAsString.ToUpperInvariant(); _compareName = Encoding.UTF8.GetBytes(CompareNameAsString); } else { CompareNameAsString = NameAsString; _compareName = _name; } // Cache the escaped name. int valueIdx = JsonWriterHelper.NeedsEscaping(_name); if (valueIdx == -1) { _escapedName = _name; } else { int length = JsonWriterHelper.GetMaxEscapedLength(_name.Length, valueIdx); byte[] tempArray = ArrayPool <byte> .Shared.Rent(length); JsonWriterHelper.EscapeString(_name, tempArray, valueIdx, out int written); _escapedName = new byte[written]; tempArray.CopyTo(_escapedName, 0); // We clear the array because it is "user data" (although a property name). new Span <byte>(tempArray, 0, written).Clear(); ArrayPool <byte> .Shared.Return(tempArray); } } }