I have a .net framework 4.7 MVC project using the NuGet Newtonsoft as a serializer. I would like to serialize a long property as a string. I am trying this solution but I cannot find what happened and does not work properly with the accepted answer.
I have a test endpoint
[RoutePrefix("Test")]
public class TestController : Controller
{
[HttpGet,Route("Test2")]
public JsonResult Test2()
{
var customer = new Customer
{
Id = long.MaxValue,
Name = "test15"
};
return new JsonResult { Data = customer, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
}
Also, I have the class person and a custom converter
public class Customer
{
[JsonConverter(typeof(Int64JsonConverter))]
public long Id { get; set; }
public string Name { get; set; }
}
class Int64JsonConverter : JsonConverter<long>
{
public override long ReadJson(JsonReader reader, Type objectType, long existingValue, bool hasExistingValue, JsonSerializer serializer)
{
if (reader.Value == null)
return 0;
return long.Parse(reader.Value.ToString());
}
public override void WriteJson(JsonWriter writer, long value, JsonSerializer serializer)
{
// convert to string instead, javascript number size is smaller than long
writer.WriteValue(value.ToString());
}
}
My first try is to annotate the property with the converter. The second try is to remove the annotation from the property and set in WebApiConfig my custom converter
public class WebApiConfig
{
public static void Register(HttpConfiguration configuration)
{
configuration.MapHttpAttributeRoutes();
configuration.Routes.MapHttpRoute(
"API Default",
"api/{controller}/{id}",
new { id = RouteParameter.Optional });
//configuration.Formatters.JsonFormatter.SerializerSettings.Converters = new List<JsonConverter> { new Int64JsonConverter(), new Int64NullableJsonConverter() };
configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new Int64JsonConverter());
}
}
Also my global asax class is
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
HttpConfiguration config = GlobalConfiguration.Configuration;
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new Int64JsonConverter());
//config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new Int64NullableJsonConverter());
}
}
I try to set the custom formatters with various ways like
//1) First try
//var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
//json.SerializerSettings.Converters = GetJsonConverters();
//2)Second try
//HttpConfiguration config = GlobalConfiguration.Configuration;
//config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new Int64JsonConverter());
//config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new Int64NullableJsonConverter());
//3) Third try
//JsonConvert.DefaultSettings = () => new JsonSerializerSettings
//{
// Formatting = Formatting.Indented,
// TypeNameHandling = TypeNameHandling.Objects,
// ContractResolver = new CamelCasePropertyNamesContractResolver(),
// Converters = GetJsonConverters()
//};
//4) Fourth try
//HttpConfiguration config = new HttpConfiguration();
//config.Formatters.JsonFormatter.SerializerSettings.Converters = new List<JsonConverter> { new Int64JsonConverter(), new Int64NullableJsonConverter() };
//5) Fifth try
//var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
//json.SerializerSettings.Converters.Add(new Int64JsonConverter());
but always the result is the long property in not converted to string I get
{
"Id": 9223372036854775807,
"Name": "test15"
}