// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) by respective owners including Yahoo!, Microsoft, and // individual contributors. All rights reserved. Released under a BSD // license as described in the file LICENSE. // // -------------------------------------------------------------------------------------------------------------------- using Newtonsoft.Json; using System; using System.Collections.Generic; namespace VW.Serializer { /// /// Custom JSON converter returning the underlying raw json (avoiding object allocation). /// public class JsonRawStringConverter : JsonConverter, IVowpalWabbitJsonConverter { /// /// Supports string only. /// public override bool CanConvert(Type objectType) { return objectType == typeof(string); } /// /// Not implemented. /// public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } /// /// Outputs the string contents as JSON. /// public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var valueString = value as string; if (valueString != null) { writer.WriteRawValue(valueString); return; } serializer.Serialize(writer, value); } /// /// List of independently parseable JSON fragments. /// public IEnumerable JsonFragments(object value) { var valueString = value as string; if (valueString != null) { yield return valueString; yield break; } } } }