Reputation: 7449
I'm saving JSON objects into the database, sometimes it grows very large (I have an object of length 205,797 characters) I want to eliminate the size as possible as I can. these objects have a lot of GUID fields, all I don't need, It may help eliminating the size if there is a way to ignore any GUID type from serializing.
This is my code, I pass an object of any model type in my application:
public static string GetEntityAsJson(object entity)
{
var json = JsonConvert.SerializeObject(entity, Formatting.None, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
return json;
}
EDIT
I don't want to use JsonIgnore
attribute, as I will have to add it to so many classes each has a lot of GUID properties,
I'm looking for something straight forward like:
IgnoreDataType = DataTypes.GUID
Upvotes: 9
Views: 5265
Reputation: 475
You can create your own Converter
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);
}
}
And use it as
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);
}
Classes
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; }
}
I used this reference to Create the converter Json.NET, how to customize serialization to insert a JSON property
Upvotes: 0
Reputation: 129657
You can use a custom ContractResolver
to ignore all properties of a specific data type in all classes. For example, here is one that ignores all 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;
}
}
To use the resolver, just add it to your JsonSerializerSettings
:
var json = JsonConvert.SerializeObject(entity, Formatting.None, new JsonSerializerSettings
{
ContractResolver = new IgnoreGuidsResolver(),
...
});
Demo fiddle: https://dotnetfiddle.net/lOOUfq
Upvotes: 7
Reputation: 318
Using [JsonIgnore]
in your entity class should solve your problem.
public class Plane
{
// included in JSON
public string Model { get; set; }
public DateTime Year { get; set; }
// ignored
[JsonIgnore]
public DateTime LastModified { get; set; }
}
Upvotes: 0