Example #1
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();
        }
Example #2
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();
                    }
                }
            }