コード例 #1
0
 public static String ToJson(this Object value)
 {
     var jsonBuilder = new StringBuilder();
     var serializer = new JsonSerializer();
     var writer = new JsonTextWriter(new StringWriter(jsonBuilder));
     serializer.Serialize(writer, value);
     return jsonBuilder.ToString();
 }
コード例 #2
0
    /// <summary>
    /// Creates a new <see cref="JsonSerializer"/> instance using the specified <see cref="JsonSerializerSettings"/> objects.
    /// </summary>
    /// <param name="settings">The settings.</param>
    /// <returns></returns>
    public static JsonSerializer Create(JsonSerializerSettings settings)
    {
      JsonSerializer jsonSerializer = new JsonSerializer();

      if (settings != null)
      {
        if (!CollectionUtils.IsNullOrEmpty(settings.Converters))
          jsonSerializer.Converters.AddRange(settings.Converters);

        jsonSerializer.ReferenceLoopHandling = settings.ReferenceLoopHandling;
        jsonSerializer.MissingMemberHandling = settings.MissingMemberHandling;
        jsonSerializer.ObjectCreationHandling = settings.ObjectCreationHandling;
        jsonSerializer.NullValueHandling = settings.NullValueHandling;
        jsonSerializer.DefaultValueHandling = settings.DefaultValueHandling;
        jsonSerializer.MappingResolver = settings.MappingResolver;
      }

      return jsonSerializer;
    }