2017-08-08 61 views
2

我正在將JSON對象保存到數據庫中,有時它會變得非常大(我有一個長度爲205,797個字符的對象)我想盡可能消除大小。這些對象有很多GUID字段,我不需要它們,如果有一種方法可以忽略任何序列化的GUID類型,它可能有助於消除大小。使用Json.NET序列化時忽略特定的數據類型?

這是我的代碼,我傳遞任何模型類型的對象,在我的應用程序:

public static string GetEntityAsJson(object entity) 
{ 
    var json = JsonConvert.SerializeObject(entity, Formatting.None, new JsonSerializerSettings 
    { 
     ReferenceLoopHandling = ReferenceLoopHandling.Ignore 
    }); 
    return json; 
} 

編輯

我不想使用JsonIgnore屬性,因爲我將不得不將它添加到許多類中,每個類都有很多GUID屬性, 我正在尋找像下面這樣的東西: IgnoreDataType = DataTypes.GUID

回答

2

您可以使用自定義ContractResolver忽略所有類的特定數據類型的所有屬性。例如,這裏是一個忽略所有Guids

class IgnoreGuidsResolver : DefaultContractResolver 
{ 
    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) 
    { 
     JsonProperty prop = base.CreateProperty(member, memberSerialization); 
     if (prop.PropertyType == typeof(Guid)) 
     { 
      prop.Ignored = true; 
     } 
     return prop; 
    } 
} 

要使用的解析器,只需將其添加到您的JsonSerializerSettings

var json = JsonConvert.SerializeObject(entity, Formatting.None, new JsonSerializerSettings 
{ 
    ContractResolver = new IgnoreGuidsResolver(), 
    ... 
}); 

演示小提琴:https://dotnetfiddle.net/lOOUfq

0

使用0123實體類中的應該可以解決您的問題。

public class Plane 
{ 
    // included in JSON 
    public string Model { get; set; } 
    public DateTime Year { get; set; } 

    // ignored 
    [JsonIgnore] 
    public DateTime LastModified { get; set; } 
} 
+0

我知道,但我將要經過幾十個有很多的GUID類的! –

+0

然後,你應該添加這個到你的問題。如果不更改類,您應該可以使用自定義合約解析器執行此操作,如下所述:https://stackoverflow.com/a/25769147/715348 –

0

您可以創建自己的轉換器

public class MyJsonConverter : JsonConverter 
{ 
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 

     JObject jo = new JObject(); 

     foreach (PropertyInfo prop in value.GetType().GetProperties()) 
     { 
      if (prop.CanRead) 
      { 
       if (prop.PropertyType == typeof(Guid)) 
        continue; 


       object propValue = prop.GetValue(value); 

       if (propValue != null) 
       { 
        jo.Add(prop.Name, JToken.FromObject(propValue)); 
       } 
      } 
     } 
     jo.WriteTo(writer); 
    } 

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     throw new NotImplementedException(); 
    } 

    public override bool CanConvert(Type objectType) 
    { 
     return objectType.IsAssignableFrom(objectType); 
    } 
} 

並將其用作

static void Main(string[] args) 
    { 
     Person testObj = new Person 
     { 
      Id = Guid.NewGuid(), 
      Name = "M.A", 
      MyAddress = new Address 
      { 
       AddressId = 1, 
       Country = "Egypt" 
      } 
     }; 

     var json = JsonConvert.SerializeObject(testObj, new MyJsonConverter()); 

     Console.WriteLine(json); 
    } 

public class Person 
{ 
    public Guid Id { get; set; } 

    public string Name { get; set; } 

    public Address MyAddress { get; set; } 

} 

public class Address 
{ 
    public int AddressId { get; set; } 

    public string Country { get; set; } 

} 

我用這個引用創建轉換 Json.NET, how to customize serialization to insert a JSON property

相關問題