Decimal properties are serialized even are not populated - c#

I had to generate some classes from an xsd file. The classes and properties are generated correct with xml serialization annotation. The problem is that the decimal properties of a class are serialized with Newtonsoft.Json even are not populated. I would like to serialize only the decimal properties that are properly populated. Amount is part of SaleMessage
For example:
class Amount
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal RequestedAmount;
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal CashBackAmount;
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal TipAmount;
}
//Usage
var amount = new Amount()
{
RequestedAmount = 12.0
}
Using this structure it will always serialize all the properties
like this
{"RequestedAmount":12.0,"CashBackAmount":0.0,"TipAmount":0.0}
Which is not the expected behaviour.
The question is how can I modify the serialization to not parse the not set properties
static string Serialize(SaleMessage saleMessage)
{
var serialize= JsonConvert.SerializeObject(saleToPoiMessage,
new StringEnumConverter(),
new IsoDateTimeConverter() { DateTimeFormat = DateTimeFormat });
return serialize;
}
Any help is appreciated :)

You can set the DefaultValueHandling setting to Ignore to suppress serialization of values that are equal to their default value.
var settings = new JsonSerializerSettings
{
Converters = new List<JsonConverter>
{
new StringEnumConverter(),
new IsoDateTimeConverter() { DateTimeFormat = DateTimeFormat }
},
DefaultValueHandling = DefaultValueHandling.Ignore
};
var json = JsonConvert.SerializeObject(saleMessage, settings);
Fiddle: https://dotnetfiddle.net/o32k0U

Since it's of type decimal primitive it will have some value by default. I think you need to implement the serializer utility Newtonsoft.Json uses - on your own. Where you will not include the decimal values of 0.0 (if this suits the business logic).
Another option is not to use the primitive class and then set up the property of removing null values while serializing. I believe you can set up this config parameter in Newtonsoft.
Check this: https://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm

In my case I changed the primitive types to nullable.
public decimal? CashBackAmount {get; set;}
This worked for me. I prefer the #Brian Rogers answer. :)

Related

NewtonSoft - Property being serialize as an integer - source is char(2) [duplicate]

I have a class that contains an enum property, and upon serializing the object using JavaScriptSerializer, my json result contains the integer value of the enumeration rather than its string "name". Is there a way to get the enum as a string in my json without having to create a custom JavaScriptConverter? Perhaps there's an attribute that I could decorate the enum definition, or object property, with?
As an example:
enum Gender { Male, Female }
class Person
{
int Age { get; set; }
Gender Gender { get; set; }
}
Desired JSON result:
{ "Age": 35, "Gender": "Male" }
Ideally looking for answer with built-in .NET framework classes, if not possible alternatives (like Json.net) are welcome.
I have found that Json.NET provides the exact functionality I'm looking for with a StringEnumConverter attribute:
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
[JsonConverter(typeof(StringEnumConverter))]
public Gender Gender { get; set; }
More details at available on StringEnumConverter documentation.
There are other places to configure this converter more globally:
enum itself if you want enum always be serialized/deserialized as string:
[JsonConverter(typeof(StringEnumConverter))]
enum Gender { Male, Female }
In case anyone wants to avoid attribute decoration, you can add the converter to your JsonSerializer (suggested by Bjørn Egil):
serializer.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
and it will work for every enum it sees during that serialization (suggested by Travis).
or JsonConverter (suggested by banana):
JsonConvert.SerializeObject(MyObject,
new Newtonsoft.Json.Converters.StringEnumConverter());
Additionally you can control casing and whether numbers are still accepted by using StringEnumConverter(NamingStrategy, Boolean) constructor.
No there is no special attribute you can use. JavaScriptSerializer serializes enums to their numeric values and not their string representation. You would need to use custom serialization to serialize the enum as its name instead of numeric value.
If you can use JSON.Net instead of JavaScriptSerializer than see answer on this question provided by Omer Bokhari: JSON.net covers this use case (via the attribute [JsonConverter(typeof(StringEnumConverter))]) and many others not handled by the built in .net serializers. Here is a link comparing features and functionalities of the serializers.
Add the below to your global.asax for JSON serialization of c# enum as string
HttpConfiguration config = GlobalConfiguration.Configuration;
config.Formatters.JsonFormatter.SerializerSettings.Formatting =
Newtonsoft.Json.Formatting.Indented;
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add
(new Newtonsoft.Json.Converters.StringEnumConverter());
#Iggy answer sets JSON serialization of c# enum as string only for ASP.NET (Web API and so).
But to make it work also with ad hoc serialization, add following to your start class (like Global.asax Application_Start)
//convert Enums to Strings (instead of Integer) globally
JsonConvert.DefaultSettings = (() =>
{
var settings = new JsonSerializerSettings();
settings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
return settings;
});
More information on the Json.NET page
Additionally, to have your enum member to serialize/deserialize to/from specific text, use the
System.Runtime.Serialization.EnumMember
attribute, like this:
public enum time_zone_enum
{
[EnumMember(Value = "Europe/London")]
EuropeLondon,
[EnumMember(Value = "US/Alaska")]
USAlaska
}
In .net core 3 this is now possible with the built-in classes in System.Text.Json (edit: System.Text.Json is also available as a NuGet package for .net core 2.0 and .net framework 4.7.2 and later versions according to the docs):
var person = new Person();
// Create and add a converter which will use the string representation instead of the numeric value.
var stringEnumConverter = new System.Text.Json.Serialization.JsonStringEnumConverter();
JsonSerializerOptions opts = new JsonSerializerOptions();
opts.Converters.Add(stringEnumConverter);
// Generate json string.
var json = JsonSerializer.Serialize<Person>(person, opts);
To configure JsonStringEnumConverter with attribute decoration for the specific property:
using System.Text.Json.Serialization;
[JsonConverter(typeof(JsonStringEnumConverter))]
public Gender Gender { get; set; }
If you want to always convert the enum as string, put the attribute at the enum itself.
[JsonConverter(typeof(JsonStringEnumConverter))]
enum Gender { Male, Female }
I wasn't able to change the source model like in the top answer (of #ob.), and I didn't want to register it globally like #Iggy. So I combined https://stackoverflow.com/a/2870420/237091 and #Iggy's https://stackoverflow.com/a/18152942/237091 to allow setting up the string enum converter on during the SerializeObject command itself:
Newtonsoft.Json.JsonConvert.SerializeObject(
objectToSerialize,
Newtonsoft.Json.Formatting.None,
new Newtonsoft.Json.JsonSerializerSettings()
{
Converters = new List<Newtonsoft.Json.JsonConverter> {
new Newtonsoft.Json.Converters.StringEnumConverter()
}
})
The combination of Omer Bokhari and uri 's answers is alsways my solution since the values that I want to provide is usually different from what I have in my enum specially that I would like to be able to change my enums if I need to.
So if anyone is interested, it is something like this:
public enum Gender
{
[EnumMember(Value = "male")]
Male,
[EnumMember(Value = "female")]
Female
}
class Person
{
int Age { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
Gender Gender { get; set; }
}
ASP.NET Core way:
public class Startup
{
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
});
}
}
https://gist.github.com/regisdiogo/27f62ef83a804668eb0d9d0f63989e3e
This is easily done by adding a ScriptIgnore attribute to the Gender property, causing it to not be serialised, and adding a GenderString property which does get serialised:
class Person
{
int Age { get; set; }
[ScriptIgnore]
Gender Gender { get; set; }
string GenderString { get { return Gender.ToString(); } }
}
This version of Stephen's answer doesn't change the name in the JSON:
[DataContract(
Namespace =
"http://schemas.datacontract.org/2004/07/Whatever")]
class Person
{
[DataMember]
int Age { get; set; }
Gender Gender { get; set; }
[DataMember(Name = "Gender")]
string GenderString
{
get { return this.Gender.ToString(); }
set
{
Gender g;
this.Gender = Enum.TryParse(value, true, out g) ? g : Gender.Male;
}
}
}
Here is the answer for newtonsoft.json
enum Gender { Male, Female }
class Person
{
int Age { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
Gender Gender { get; set; }
}
Asp.Net Core 3 with System.Text.Json
public void ConfigureServices(IServiceCollection services)
{
services
.AddControllers()
.AddJsonOptions(options =>
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter())
);
//...
}
You can also add a converter to your JsonSerializer if you don't want to use JsonConverter attribute:
string SerializedResponse = JsonConvert.SerializeObject(
objToSerialize,
new Newtonsoft.Json.Converters.StringEnumConverter()
);
It will work for every enum it sees during that serialization.
Here is a simple solution that serializes a server-side C# enum to JSON and uses the result to populate a client-side <select> element. This works for both simple enums and bitflag enums.
I have included the end-to-end solution because I think most people wanting to serialize a C# enum to JSON will also probably be using it to fill a <select> drop-down.
Here goes:
Example Enum
public enum Role
{
None = Permission.None,
Guest = Permission.Browse,
Reader = Permission.Browse| Permission.Help ,
Manager = Permission.Browse | Permission.Help | Permission.Customise
}
A complex enum that uses bitwise ORs to generate a permissions system. So you can't rely on the simple index [0,1,2..] for the integer value of the enum.
Server Side - C#
Get["/roles"] = _ =>
{
var type = typeof(Role);
var data = Enum
.GetNames(type)
.Select(name => new
{
Id = (int)Enum.Parse(type, name),
Name = name
})
.ToArray();
return Response.AsJson(data);
};
The code above uses the NancyFX framework to handle the Get request. It uses Nancy's Response.AsJson() helper method - but don't worry, you can use any standard JSON formatter as the enum has already been projected into a simple anonymous type ready for serialization.
Generated JSON
[
{"Id":0,"Name":"None"},
{"Id":2097155,"Name":"Guest"},
{"Id":2916367,"Name":"Reader"},
{"Id":4186095,"Name":"Manager"}
]
Client Side - CoffeeScript
fillSelect=(id, url, selectedValue=0)->
$select = $ id
$option = (item)-> $ "<option/>",
{
value:"#{item.Id}"
html:"#{item.Name}"
selected:"selected" if item.Id is selectedValue
}
$.getJSON(url).done (data)->$option(item).appendTo $select for item in data
$ ->
fillSelect "#role", "/roles", 2916367
HTML Before
<select id="role" name="role"></select>
HTML After
<select id="role" name="role">
<option value="0">None</option>
<option value="2097155">Guest</option>
<option value="2916367" selected="selected">Reader</option>
<option value="4186095">Manager</option>
</select>
For ASP.Net core Just add the following to your Startup Class:
JsonConvert.DefaultSettings = (() =>
{
var settings = new JsonSerializerSettings();
settings.Converters.Add(new StringEnumConverter { AllowIntegerValues = false });
return settings;
});
For .NET 6.0 if you want to use the built-in JsonSerializer (System.Text.Json)
Then, it comes out-of-the-box, you just need to use the built-in JsonStringEnumConverter attribute. For example:
[JsonConverter(typeof(JsonStringEnumConverter))]
public SomeEnumType EnumProperty { get; set; }
And that's it, BUT make sure your SomeEnumType contains values with the exact string values, otherwise it will throw an exception. Casing seems to be insensitive.
Reference: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-customize-properties?pivots=dotnet-6-0#enums-as-strings
You can create JsonSerializerSettings with the call to JsonConverter.SerializeObject as below:
var result = JsonConvert.SerializeObject
(
dataObject,
new JsonSerializerSettings
{
Converters = new [] {new StringEnumConverter()}
}
);
Noticed that there is no answer for serialization when there is a Description attribute.
Here is my implementation that supports the Description attribute.
public class CustomStringEnumConverter : Newtonsoft.Json.Converters.StringEnumConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Type type = value.GetType() as Type;
if (!type.IsEnum) throw new InvalidOperationException("Only type Enum is supported");
foreach (var field in type.GetFields())
{
if (field.Name == value.ToString())
{
var attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
writer.WriteValue(attribute != null ? attribute.Description : field.Name);
return;
}
}
throw new ArgumentException("Enum not found");
}
}
Enum:
public enum FooEnum
{
// Will be serialized as "Not Applicable"
[Description("Not Applicable")]
NotApplicable,
// Will be serialized as "Applicable"
Applicable
}
Usage:
[JsonConverter(typeof(CustomStringEnumConverter))]
public FooEnum test { get; set; }
For .Net Core :-
public void ConfigureServices(IServiceCollection services)
{
...
services.AddJsonFormatters(f => f.Converters.Add(new StringEnumConverter()));
...
}
Use this:
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
[Serializable]
[JsonConverter(typeof(StringEnumConverter))]
public enum Gender { Male, Female }
Just in case anybody finds the above insufficient, I ended up settling with this overload:
JsonConvert.SerializeObject(objToSerialize, Formatting.Indented, new Newtonsoft.Json.Converters.StringEnumConverter())
This is an old question but I thought I'd contribute just in case. In my projects I use separate models for any Json requests. A model would typically have same name as domain object with "Json" prefix. Models are mapped using AutoMapper. By having the json model declare a string property that is an enum on domain class, AutoMapper will resolve to it's string presentation.
In case you are wondering, I need separate models for Json serialized classes because inbuilt serializer comes up with circular references otherwise.
Hope this helps someone.
You can actually use a JavaScriptConverter to accomplish this with the built-in JavaScriptSerializer. By converting your enum to a Uri you can encode it as a string.
I've described how to do this for dates but it can be used for enums as well.
Custom DateTime JSON Format for .NET JavaScriptSerializer.
A slightly more future-proof option
Facing the same question, we determined that we needed a custom version of StringEnumConverter to make sure that our enum values could expand over time without breaking catastrophically on the deserializing side (see background below). Using the SafeEnumConverter below allows deserialization to finish even if the payload contains a value for the enum that does not have a named definition, closer to how int-to-enum conversion would work.
Usage:
[SafeEnumConverter]
public enum Colors
{
Red,
Green,
Blue,
Unsupported = -1
}
or
[SafeEnumConverter((int) Colors.Blue)]
public enum Colors
{
Red,
Green,
Blue
}
Source:
public class SafeEnumConverter : StringEnumConverter
{
private readonly int _defaultValue;
public SafeEnumConverter()
{
// if you've been careful to *always* create enums with `0` reserved
// as an unknown/default value (which you should), you could use 0 here.
_defaultValue = -1;
}
public SafeEnumConverter(int defaultValue)
{
_defaultValue = defaultValue;
}
/// <summary>
/// Reads the provided JSON and attempts to convert using StringEnumConverter. If that fails set the value to the default value.
/// </summary>
/// <returns>The deserialized value of the enum if it exists or the default value if it does not.</returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
try
{
return base.ReadJson(reader, objectType, existingValue, serializer);
}
catch
{
return Enum.Parse(objectType, $"{_defaultValue}");
}
}
public override bool CanConvert(Type objectType)
{
return base.CanConvert(objectType) && objectType.GetTypeInfo().IsEnum;
}
}
Background
When we looked at using the StringEnumConverter, the problem we had is that we also needed passivity for cases when a new enum value was added, but not every client was immediately aware of the new value. In these cases, the StringEnumConverter packaged with Newtonsoft JSON throws a JsonSerializationException similar to "Error converting value SomeString to type EnumType" and then the whole deserialization process fails. This was a deal breaker for us, because even if the client planned on ignoring/discarding the property value that it didn't understand, it still needed to be capable of deserializing the rest of the payload!
Not sure if this is still relevant but I had to write straight to a json file and I came up with the following piecing several stackoverflow answers together
public class LowercaseJsonSerializer
{
private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
ContractResolver = new LowercaseContractResolver()
};
public static void Serialize(TextWriter file, object o)
{
JsonSerializer serializer = new JsonSerializer()
{
ContractResolver = new LowercaseContractResolver(),
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore
};
serializer.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
serializer.Serialize(file, o);
}
public class LowercaseContractResolver : DefaultContractResolver
{
protected override string ResolvePropertyName(string propertyName)
{
return Char.ToLowerInvariant(propertyName[0]) + propertyName.Substring(1);
}
}
}
It assures all my json keys are lowercase starting according to json "rules". Formats it cleanly indented and ignores nulls in the output. Aslo by adding a StringEnumConverter it prints enums with their string value.
Personally I find this the cleanest I could come up with, without having to dirty the model with annotations.
usage:
internal void SaveJson(string fileName)
{
// serialize JSON directly to a file
using (StreamWriter file = File.CreateText(#fileName))
{
LowercaseJsonSerializer.Serialize(file, jsonobject);
}
}
And for VB.net I found the following works:
Dim sec = New Newtonsoft.Json.Converters.StringEnumConverter()
sec.NamingStrategy() = New Serialization.CamelCaseNamingStrategy
Dim JSON_s As New JsonSerializer
JSON_s.Converters.Add(sec)
Dim jsonObject As JObject
jsonObject = JObject.FromObject(SomeObject, JSON_s)
Dim text = jsonObject.ToString
IO.File.WriteAllText(filePath, text)
I have put together all of the pieces of this solution using the Newtonsoft.Json library. It fixes the enum issue and also makes the error handling much better, and it works in IIS hosted services. It's quite a lot of code, so you can find it on GitHub here: https://github.com/jongrant/wcfjsonserializer/blob/master/NewtonsoftJsonFormatter.cs
You have to add some entries to your Web.config to get it to work, you can see an example file here:
https://github.com/jongrant/wcfjsonserializer/blob/master/Web.config
For anyone needing a solution in May of '22 for .NET 6 and still using Newtonsoft, you can register the converter globally like this:
var builder = WebApplication.CreateBuilder(args);
...
builder.Services.AddControllers(options => options.RespectBrowserAcceptHeader = true)
.AddNewtonsoftJson(opt =>
{
opt.SerializerSettings.ContractResolver = new DefaultContractResolver();
opt.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
})
.AddXmlSerializerFormatters()
.AddXmlDataContractSerializerFormatters();
The namespace System.Text.Json.Serialization has JsonStringEnumConverter which can be used like below.
[JsonConverter(typeof(JsonStringEnumConverter))]
Person p = new Person();
p.Age = 35;
p.Gender = Gender.Male;
//1. male="Male";
string male = Gender.Male.ToString();
p.Gender = Gender.Female;
//2. female="Female";
string female = Enum.GetName(typeof(Gender), p.Gender);
JObject jobj = new JObject();
jobj["Age"] = p.Age;
jobj["Gender"] = male;
jobj["Gender2"] = female;
//you result: josn= {"Age": 35,"Gender": "Male","Gender2": "Female"}
string json = jobj.ToString();

Default numeric value from JSON property with empty string

I'm trying to deserialize a JSON string, which contains an empty string value for one property... that property should be parsed into a decimal property of the class...
public class myClass {
public decimal my_value { get; set; } = 0;
}
var json = "{ \"my_value\": \"\" }";
var data = JsonConvert.DeserializeObject<myClass>(json);
The issue is that data is coming back as a null object.
I've tried setting the property to be decimal?, and that does return an object, but my_value is null when I need it to default to 0.
I've also tried the following, but it returns a null object (using either decimal or decimal?)...
[System.ComponentModel.DefaultValue(0)]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
public decimal my_value { get; set; } = 0;
How can I set this up to default to a value of 0 if the JSON contains an empty string for the property?
Before anybody states it... yes, the easy answer is to not have an empty string property in the JSON, but there is nothing I can do about it
You need to set up your serialization settings to ignore null values.
the settings would look like this if you are using Newtonsoft. You can learn more here
var = jsonSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
}

How to refine json by remove null values properties and zero (0) value properties from the c# object

Scenario:
I have to send a Httpwebrequest and server demands it will accept only two values as Json format, I want to send one more request to another server and that demands one value at a time in Json format.
For above scenario I created a Class and provide all three properties like following
pubilc class MyClass
{
public string as { get; set;}
public int value { get; set;}
public string asd { get;s et;}
}
For the first HttpWebRequest, to the first server, I want to send only two properties from MyClass 'as' and 'asd' now I will serialize through JsonConvert function of NewtonSoft as following
MyClass class = new MyClass();
string json = JsonConvert.SerializeObject(class);
The above syntax will return json having 0 and null values properties, NewtonSoft provide the functions to remove the null value from Json but it can't remove the properties having value 0, or you can say if your property data type is int and there is no any value assigned than it assign 0 to those properties.
Syntax to remove Null properties from Json
string json = JsonConvert.SerializeObject(class, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
The above syntax will remove the null values during serialize the MyClass object.
Now Question how to remove properties from json if it has properties having 0.
You can try to define your int property as nullable:
public int? value { get; set;}
Just modify your expression
string json = JsonConvert.SerializeObject(
class,
Newtonsoft.Json.Formatting.Indented,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }
);
as below,
string json = JsonConvert.SerializeObject(
class,
Newtonsoft.Json.Formatting.Indented,
new JsonSerializerSettings {
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore
}
);
Answer given by mgigirey accepted by me, Another solution which i did, I converted my Json into xml and removed the nodes having value 0 and again converted into Json. My solution is also working for me but it is bit lengthy and slower then Mgigirey answer.
So two answers are here, if you anyone want to go with my solution then see following.
//This line will remove the null as i earlier mentioned in my question.
string jsonStatus = JsonConvert.SerializeObject(myJson, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
//Create xml object by covnert json into xml
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
//Get the property name which have the value 0
var v = doc.GetElementsByTagName("value")[0];
//Remove the child node.
doc.DocumentElement.RemoveChild(v);
//Again convert into the json.
string jsonText = JsonConvert.SerializeObject(doc);

Newtonsoft.JSON Serialization Array, Object, or Null

I have some JSON that can be a List or null. How do I create a POCO for this JSON?
Here is an example array:
http://pastebin.com/qAZF2Ug9
Here is my POCO:
http://pastebin.com/hUtgyytc
How can I tell Newtonsoft.JSON to ignore the SalesLine object, if it is null?
You can specify the settings:
var settings = new Newtonsoft.Json.JsonSerializerSettings {
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore};
and use that in various serializer constructors and serialize calls.
Alternatively, IIRC it supports conditional serialization, i.e.
public bool ShouldSerializeFoo() { return Foo != null; }
// pairs to property Foo
Try to mark this property with JsonProperty attribute
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public SaleLines SaleLines { get; set; }

Using JSON.NET, how do I serialize these inherited members?

I have the following:
public class MyClass : SuperClass {
[JsonProperty]
public virtual string Id { get; set; }
}
public abstract class SuperClass {
public int GetHashCode() {
//do things here
}
}
I cannot alter SuperClass. When I go to serialize to Json using JsonNet I'll do something like this:
JsonSerializerSettings serializer = new JsonSerializerSettings {
//serializer settings
};
var jsonNetResult = new JsonNetResult {
Data = myClass,
SerializerSettings = serializer
};
return jsonNetResult;
Obviously it will not serialize GetHashCode(). If I go:
var jsonNetResult = new JsonNetResult {
Data = myClass.GetHashCode(),
SerializerSettings = serializer
};
It will correctly serialize the value, is there some serializer setting I can use to tell it to include GetHashCode()?
Edit: I should add that right now I'm creating a property with only get to accomplish this, i.e.
[JsonProperty]
public virtual int GetHashCodeJson { get { return GetHashCode(); }
This is not so much an issue with JSON.Net as with .net serialization in general.
You need to serialize objects by their properties and you are asking to serialize the return value of a method. So there is not a way to do this with the syntax you want.
That you are able to do this:
Data = myClass.GetHashCode()
Only means that the return value of the method (an int) can be serialized and not that the serializer cares at all about what method that value is coming from.
If you think about it, there is not a lot of sense to saying that a value is the serialized return value of a method because how do you deserialize that then? You would never be able to write the value back to the method because its a return value only, not a 2-way relationship like a property with {get;set;}.

Categories