Exemple #1
0
        /// <inheritdoc />
        public override void WriteJson(JsonWriter writer, object value, global::Newtonsoft.Json.JsonSerializer jsonSerializer)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }

            Type type = value.AsType();

            if (!IsTypeIncluded(type))
            {
                jsonSerializer.Serialize(writer, value);
                return;
            }

            JToken token = JToken.FromObject(value);

            if (token.Type != JTokenType.Object)
            {
                token.WriteTo(writer);
            }
            else
            {
                JObject jObject = (JObject)token;
                jObject.AddFirst(new JProperty("keys", new JArray(jObject.Properties().Select(e => e.Name))));
                jObject.WriteTo(writer);
            }
        }
Exemple #2
0
        public override void WriteJson(JsonWriter writer, object value, global::Newtonsoft.Json.JsonSerializer serializer)
        {
            var collection     = (DocumentCollection)value;
            var jsonCollection = new JsonDocumentCollection(collection);

            serializer.Serialize(writer, jsonCollection);
        }
Exemple #3
0
        /// <summary>
        /// Serialize the object as JSON
        /// </summary>
        /// <param name="obj">Object to serialize</param>
        /// <returns>JSON as String</returns>
        public string Serialize(object obj)
        {
            using (var stringWriter = new StringWriter())
            {
                using (var jsonTextWriter = new JsonTextWriter(stringWriter))
                {
                    _serializer.Serialize(jsonTextWriter, obj);

                    return(stringWriter.ToString());
                }
            }
        }
Exemple #4
0
 public string Serialize(object paramObj)
 {
     using (var localStringWriter = new StringWriter())
     {
         using (var localJsonTextWriter = new JsonTextWriter(localStringWriter))
         {
             localJsonTextWriter.Formatting = Formatting.None;
             localJsonTextWriter.QuoteChar  = '"';
             instanceSerializer.Serialize(localJsonTextWriter, paramObj);
             var localResult = localStringWriter.ToString();
             return(localResult);
         }
     }
 }
Exemple #5
0
        public override void WriteJson(JsonWriter writer, object value, global::Newtonsoft.Json.JsonSerializer serializer)
        {
            // The container should be always a JSON
            var contract = serializer.ContractResolver.ResolveContract(value.GetType()) as JsonObjectContract;

            if (contract == null)
            {
                return;
            }

            var shouldStartObject = writer.WriteState == WriteState.Start || writer.WriteState == WriteState.Array || writer.WriteState == WriteState.Property;

            if (shouldStartObject)
            {
                writer.WriteStartObject();
            }

            foreach (var property in contract.Properties.Where(p => p.ShouldSerialize == null || p.ShouldSerialize(value)))
            {
                var propertyValue = property.ValueProvider.GetValue(value);
                if (propertyValue == null)
                {
                    continue;
                }

                if (property.DefaultValueHandling == DefaultValueHandling.Ignore &&
                    propertyValue.Equals(property.DefaultValue ?? property.PropertyType.GetDefaultValue()))
                {
                    continue;
                }

                writer.WritePropertyName(property.PropertyName);

                var document = propertyValue as Document;
                if (document != null && !document.GetMediaType().IsJson)
                {
                    writer.WriteValue(document.ToString());
                }
                else
                {
                    serializer.Serialize(writer, propertyValue);
                }
            }

            if (shouldStartObject)
            {
                writer.WriteEndObject();
            }
        }
Exemple #6
0
        /// <summary>
        /// Serialize the object as JSON
        /// </summary>
        /// <param name="obj">Object to serialize</param>
        /// <returns>JSON as String</returns>
        public string Serialize(object obj)
        {
            using (var stringWriter = new StringWriter())
            {
                using (var jsonTextWriter = new JsonTextWriter(stringWriter))
                {
                    jsonTextWriter.Formatting = Formatting.Indented;
                    jsonTextWriter.QuoteChar  = '"';

                    _serializer.Serialize(jsonTextWriter, obj);

                    var result = stringWriter.ToString();
                    return(result);
                }
            }
        }
Exemple #7
0
 public override void WriteJson(global::Newtonsoft.Json.JsonWriter writer, PackageIdentifier value, global::Newtonsoft.Json.JsonSerializer serializer)
 {
     if (writer is null)
     {
         throw new global::System.ArgumentNullException(nameof(writer));
     }
     if (value is null)
     {
         throw new global::System.ArgumentNullException(nameof(value));
     }
     if (serializer is null)
     {
         throw new global::System.ArgumentNullException(nameof(serializer));
     }
     serializer.Serialize(writer, value._value);
 }
Exemple #8
0
        public override void WriteJson(global::Newtonsoft.Json.JsonWriter writer, [AllowNull] IConfigurationDictionary <TElement> value, global::Newtonsoft.Json.JsonSerializer serializer)
        {
            if (ReferenceEquals(value, null))
            {
                writer.WriteNull();
                return;
            }

            writer.WriteStartObject();

            foreach (var item in value)
            {
                writer.WritePropertyName(item.Key);
                serializer.Serialize(writer, item.Value);
            }

            writer.WriteEndObject();
        }
Exemple #9
0
            public override void WriteJson(global::Newtonsoft.Json.JsonWriter writer, object value, global::Newtonsoft.Json.JsonSerializer serializer)
            {
                var message = (Message)value;

                if (message.Type.IsJson)
                {
                    if (message.Content is JsonDocument)
                    {
                        throw new NotSupportedException("The content type is not supported by this serializer");
                    }

                    serializer.Serialize(writer, value);
                }
                else
                {
                    writer.WriteStartObject();

                    writer.WriteValueIfNotDefault(Envelope.ID_KEY, message.Id);
                    writer.WriteValueIfNotDefaultAsString(Envelope.FROM_KEY, message.From);
                    writer.WriteValueIfNotDefaultAsString(Envelope.TO_KEY, message.To);
                    writer.WriteValueIfNotDefaultAsString(Envelope.PP_KEY, message.Pp);

                    writer.WritePropertyName(Message.TYPE_KEY);
                    writer.WriteValue(message.Type.ToString());
                    writer.WritePropertyName(Message.CONTENT_KEY);
                    writer.WriteValue(message.Content.ToString());

                    if (message.Metadata != null)
                    {
                        writer.WritePropertyName(Message.METADATA_KEY);
                        writer.WriteStartObject();

                        foreach (var item in message.Metadata)
                        {
                            writer.WritePropertyName(item.Key);
                            writer.WriteValue(item.Value);
                        }

                        writer.WriteEndObject();
                    }
                }
            }
        public override void WriteJson(JsonWriter writer, object value, global::Newtonsoft.Json.JsonSerializer serializer)
        {
            var document = value as Document;

            if (document != null)
            {
                if (document.GetMediaType().IsJson)
                {
                    // TODO: Any document inside the value will not be correct handled.
                    _alternativeSerializer.Serialize(writer, document);
                }
                else
                {
                    writer.WriteValue(document.ToString());
                }
            }
            else
            {
                writer.WriteNull();
            }
        }
Exemple #11
0
        public override void WriteJson(global::Newtonsoft.Json.JsonWriter writer, [AllowNull] IConfigurationCollection <TElement> value, global::Newtonsoft.Json.JsonSerializer serializer)
        {
            if (ReferenceEquals(value, null))
            {
                writer.WriteNull();
                return;
            }

            writer.WriteStartArray();

            foreach (var item in value)
            {
                if (ReferenceEquals(value, null))
                {
                    writer.WriteNull();
                }
                else
                {
                    serializer.Serialize(writer, item);
                }
            }

            writer.WriteEndArray();
        }
Exemple #12
0
 public override void WriteJson(global::Newtonsoft.Json.JsonWriter writer, object value, global::Newtonsoft.Json.JsonSerializer serializer)
 {
     // TODO: Implement DocumentCollection serialization
     serializer.Serialize(writer, value);
 }