/// <summary>
        /// Uses the specified converter to create a JSON representation as a string.
        /// </summary>
        /// <param name="instance">an instance of some type</param>
        /// <param name="converter">a converter for that type</param>
        /// <returns>the serialized JSON data as a string</returns>
        public static string SerializeObject(object instance, IJsonStreamConverter converter)
        {
            var writer = JWriter.New();

            SerializeObjectToJWriter(instance, writer, converter);
            return(writer.GetString());
        }
        /// <summary>
        /// Decodes a value from a JSON representation using the specified converter.
        /// </summary>
        /// <param name="json">the JSON representation as a string</param>
        /// <param name="converter">a converter for the desired type</param>
        /// <returns>an instance of that type</returns>
        /// <exception cref="JsonReadException">if an error occurred in parsing
        /// the JSON or translating it to the desired type; see subclasses of
        /// <see cref="JsonReadException"/> for more specific errors</exception>
        public static object DeserializeObject(string json, IJsonStreamConverter converter)
        {
            var reader = JReader.FromString(json);

            try
            {
                return(converter.ReadJson(ref reader));
            }
            catch (Exception ex)
            {
                throw reader.TranslateException(ex);
            }
        }
Exemple #3
0
        /// <summary>
        /// Creates the attribute.
        /// </summary>
        /// <param name="converterType">a type that implements <see cref="IJsonStreamConverter"/></param>
        public JsonStreamConverterAttribute(Type converterType)
#if USE_SYSTEM_TEXT_JSON
            : base(typeof(JsonStreamConverterSystemTextJson))
            // Calling the constructor for the base class here means that the class has an implied
            // attribute of [JsonConverter(typeof(JsonStreamConverter))], so System.Text.Json will
            // know to delegate to our logic.
#endif
        {
            if (!typeof(IJsonStreamConverter).IsAssignableFrom(converterType))
            {
                throw new ArgumentException("type for JsonStreamConverterAttribute must implement IJsonStreamConverter");
            }
            var ctor = converterType.GetConstructor(Type.EmptyTypes);

            if (ctor is null)
            {
                throw new ArgumentException("type for JsonStreamConverterAttribute must have a no-argument public constructor");
            }
            Converter = ctor.Invoke(null) as IJsonStreamConverter;
        }
 private static void SerializeObjectToJWriter(object instance, IValueWriter writer, IJsonStreamConverter converter)
 {
     if (converter is null)
     {
         throw new NullReferenceException(nameof(converter));
     }
     if (instance is null)
     {
         writer.Null();
     }
     else
     {
         converter.WriteJson(instance, writer);
     }
 }
 public ConverterImpl(IJsonStreamConverter jsonStreamConverter)
 {
     _jsonStreamConverter = jsonStreamConverter;
 }