Exemplo n.º 1
0
    private void SerializeDictionary(JsonWriter writer, IDictionary values)
    {
      writer.WriteStartObject();

      foreach (DictionaryEntry entry in values)
      {
        writer.WritePropertyName(entry.Key.ToString());
        SerializeValue(writer, entry.Value, null);
      }

      writer.WriteEndObject();
    }
Exemplo n.º 2
0
 /// <summary>
 /// Writes the JSON representation of the object.
 /// </summary>
 /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
 /// <param name="value">The value.</param>
 public abstract void WriteJson(JsonWriter writer, object value);
Exemplo n.º 3
0
 private void SerializeCollection(JsonWriter writer, ICollection values)
 {
   SerializeList(writer, values.Cast<object>().ToList());
 }
Exemplo n.º 4
0
    private void SerializeList(JsonWriter writer, IList values)
    {
      writer.WriteStartArray();

      for (int i = 0; i < values.Count; i++)
      {
        SerializeValue(writer, values[i], null);
      }

      writer.WriteEndArray();
    }
Exemplo n.º 5
0
 private void SerializeEnumerable(JsonWriter writer, IEnumerable values)
 {
   SerializeList(writer, values.Cast<object>().ToList());
 }
Exemplo n.º 6
0
    private void SerializeObject(JsonWriter writer, object value)
    {
      Type objectType = value.GetType();

#if !SILVERLIGHT && !PocketPC
      TypeConverter converter = TypeDescriptor.GetConverter(objectType);

      // use the objectType's TypeConverter if it has one and can convert to a string
      if (converter != null && !(converter is ComponentConverter) && (converter.GetType() != typeof(TypeConverter) || value is Type))
      {
        if (converter.CanConvertTo(typeof(string)))
        {
          writer.WriteValue(converter.ConvertToInvariantString(value));
          return;
        }
      }
#else
      if (value is Guid || value is Type)
      {
        writer.WriteValue(value.ToString());
        return;
      }
#endif

      writer.SerializeStack.Add(value);

      writer.WriteStartObject();

      JsonMemberMappingCollection memberMappings = GetMemberMappings(objectType);

      foreach (JsonMemberMapping memberMapping in memberMappings)
      {
        if (!memberMapping.Ignored && memberMapping.Readable)
          WriteMemberInfoProperty(writer, value, memberMapping);
      }

      writer.WriteEndObject();

      writer.SerializeStack.RemoveAt(writer.SerializeStack.Count - 1);
    }
Exemplo n.º 7
0
    private void WriteMemberInfoProperty(JsonWriter writer, object value, JsonMemberMapping memberMapping)
    {
      MemberInfo member = memberMapping.Member;
      string propertyName = memberMapping.MappingName;
      JsonConverter memberConverter = memberMapping.MemberConverter;
      object defaultValue = memberMapping.DefaultValue;

      if (!ReflectionUtils.IsIndexedProperty(member))
      {
        object memberValue = ReflectionUtils.GetMemberValue(member, value);

        if (_nullValueHandling == NullValueHandling.Ignore && memberValue == null)
          return;

        if (_defaultValueHandling == DefaultValueHandling.Ignore && object.Equals(memberValue, defaultValue))
          return;

        if (writer.SerializeStack.IndexOf(memberValue) != -1)
        {
          switch (_referenceLoopHandling)
          {
            case ReferenceLoopHandling.Error:
              throw new JsonSerializationException("Self referencing loop");
            case ReferenceLoopHandling.Ignore:
              // return from method
              return;
            case ReferenceLoopHandling.Serialize:
              // continue
              break;
            default:
              throw new InvalidOperationException("Unexpected ReferenceLoopHandling value: '{0}'".FormatWith(CultureInfo.InvariantCulture, _referenceLoopHandling));
          }
        }

        writer.WritePropertyName(propertyName ?? member.Name);
        SerializeValue(writer, memberValue, memberConverter);
      }
    }
Exemplo n.º 8
0
    private void SerializeValue(JsonWriter writer, object value, JsonConverter memberConverter)
    {
      JsonConverter converter;

      if (value == null)
      {
        writer.WriteNull();
      }
      else if (memberConverter != null)
      {
        memberConverter.WriteJson(writer, value);
      }
      else if (HasClassConverter(value.GetType(), out converter))
      {
        converter.WriteJson(writer, value);
      }
      else if (HasMatchingConverter(value.GetType(), out converter))
      {
        converter.WriteJson(writer, value);
      }
      else if (JsonConvert.IsJsonPrimitive(value))
      {
        writer.WriteValue(value);
      }
      else if (value is IList)
      {
        SerializeList(writer, (IList)value);
      }
      else if (value is IDictionary)
      {
        SerializeDictionary(writer, (IDictionary)value);
      }
      else if (value is ICollection)
      {
        SerializeCollection(writer, (ICollection)value);
      }
      else if (value is IEnumerable)
      {
        SerializeEnumerable(writer, (IEnumerable)value);
      }
      else if (value is JsonRaw)
      {
        writer.WriteRawValue(((JsonRaw)value).Content);
      }
      else
      {
        SerializeObject(writer, value);
      }
    }
Exemplo n.º 9
0
    /// <summary>
    /// Serializes the specified <see cref="Object"/> and writes the Json structure
    /// to a <c>Stream</c> using the specified <see cref="JsonWriter"/>. 
    /// </summary>
    /// <param name="jsonWriter">The <see cref="JsonWriter"/> used to write the Json structure.</param>
    /// <param name="value">The <see cref="Object"/> to serialize.</param>
    public void Serialize(JsonWriter jsonWriter, object value)
    {
      if (jsonWriter == null)
        throw new ArgumentNullException("jsonWriter");

      if (value is JToken)
        ((JToken)value).WriteTo(jsonWriter, (_converters != null) ? _converters.ToArray() : null);
      else
        SerializeValue(jsonWriter, value, null);
    }