/// <summary>
 /// Default serializer
 /// </summary>
 public NewtonsoftJsonSerializer()
 {
     ContentType     = "application/json";
     this.serializer = new JsonSerializer();
     this.serializer.MissingMemberHandling = MissingMemberHandling.Ignore;
     this.serializer.NullValueHandling     = NullValueHandling.Include;
     this.serializer.DefaultValueHandling  = DefaultValueHandling.Include;
 }
示例#2
0
        public JsonMessageSerializer(
            IMessageMapper messageMapper,
            Func <Stream, JsonReader> readerCreator,
            Func <Stream, JsonWriter> writerCreator,
            JsonSerializerSettings settings,
            string contentType)
        {
            this.messageMapper = messageMapper;

            settings = settings ?? new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.Auto
            };

            this.writerCreator = writerCreator ?? (stream =>
            {
                var streamWriter = new StreamWriter(stream, Encoding.UTF8);
                return(new JsonTextWriter(streamWriter)
                {
                    Formatting = Formatting.None
                });
            });

            this.readerCreator = readerCreator ?? (stream =>
            {
                var streamReader = new StreamReader(stream, Encoding.UTF8);
                return(new JsonTextReader(streamReader));
            });

            if (contentType == null)
            {
                ContentType = ContentTypes.Json;
            }
            else
            {
                ContentType = contentType;
            }
            jsonSerializer = NewtonSerializer.Create(settings);
        }
 /// <summary>
 /// Default serializer with overload for allowing custom Json.NET settings
 /// </summary>
 public NewtonsoftJsonSerializer(JsonSerializer serializer)
 {
     ContentType     = "application/json";
     this.serializer = serializer;
 }
    public override void WriteJson(JsonWriter writer, object value, NewtonsoftJsonSerializer serializer)
    {
        var xcontainer = (XContainer)value;

        writer.WriteValue(xcontainer.ToString(SaveOptions.DisableFormatting));
    }
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, NewtonsoftJsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
        {
            return(null);
        }

        if (reader.TokenType != JsonToken.String)
        {
            throw new Exception($"Unexpected token or value when parsing XContainer. Token: {reader.TokenType}, Value: {reader.Value}");
        }

        var value = (string)reader.Value;

        if (objectType == typeof(XDocument))
        {
            try
            {
                return(XDocument.Load(new StringReader(value)));
            }
            catch (Exception exception)
            {
                throw new Exception($"Error parsing XContainer string: {reader.Value}", exception);
            }
        }

        return(XElement.Load(new StringReader(value)));
    }