|
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace NuGet.Protocol.Plugins
{
internal sealed class RawJsonStringConverter : JsonConverter<string>
{
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}
if (reader.TokenType != JsonTokenType.StartObject)
{
throw new JsonException(string.Format(CultureInfo.CurrentCulture, Strings.Error_UnexpectedPayloadTokenType, reader.TokenType));
}
using var doc = JsonDocument.ParseValue(ref reader);
return doc.RootElement.GetRawText();
}
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
if (value is null)
{
writer.WriteNullValue();
}
else
{
writer.WriteRawValue(value);
}
}
}
}
|