|
// 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.
#nullable disable
using System;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace NuGet.Protocol.Plugins
{
internal sealed class NsjRawJsonStringConverter : JsonConverter
{
public override bool CanConvert(Type objectType) => objectType == typeof(string);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
{
return null;
}
if (reader.TokenType != JsonToken.StartObject)
{
throw new JsonSerializationException(
string.Format(CultureInfo.CurrentCulture, Strings.Error_UnexpectedJsonToken, reader.TokenType));
}
var obj = JObject.Load(reader);
return obj.ToString(Formatting.None);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value is string s)
{
JObject.Parse(s).WriteTo(writer);
}
else
{
writer.WriteNull();
}
}
}
}
|