コード例 #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();
    }
コード例 #2
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);
    }