Send javascript function via json using Json.NET lib

I am trying to send a javascript function via json to .Net and I am having a problem serializing the object.

The javascript Highcharts library uses the following function in the json object to customize the chart tooltip.

tooltip: { formatter: function() { var s; if (this.point.name) { // the pie chart s = ''+ this.point.name +': '+ this.y +' fruits'; } else { s = ''+ this.x +': '+ this.y; } return s; } }, 

I am trying to use the popular Json.NET library using an anonymous type to create such an object, but all my efforts are converted to a string at the end. Any help is appreciated. Thank you

+4
source share
4 answers

JSON, as the name implies, is only an object designation and is suitable for transferring state as an object.

If you need to send JavaScript code yourself, you can use JavaScriptResult in ASP NET MVC if you use it.

If you are using ASP NET Web Forms, you have an ASPX file that directly writes JavaScript. Make sure you change the value of ContentType to text/javascript .

+3
source

A bit of a hack, but I solved it like this:

 string linkUrl = "http://www.google.com"; dynamic result = new ExpandoObject(); result.render = "FUNCfunction (data) { return '<a href=\"" + linkUrl + "\">' + data + '</a>'; }FUNC"; var json = Newtonsoft.Json.JsonConvert.SerializeObject(result).Replace("\"FUNC", "").Replace("FUNC\"", "") 

By surrounding my function with FUNC placeholders, and then replacing "FUNC and FUNC" with the emtpy string in the generated json, I get the function in my json object.

+2
source

I like the answer from ObOzOne, but it can be a little simpler just using WriteRawValue. For instance:

 public class FunctionSerializer : JsonConverter { public override bool CanConvert(Type objectType) { return (objectType == typeof(string)); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { string valueAsString = Convert.ToString(value); if (!string.IsNullOrWhiteSpace(valueAsString)) writer.WriteRawValue(valueAsString); } } 

And then on your property you want to serialize:

 [JsonConverter(typeof(FunctionSerializer))] public string HideExpression { get; set; } 
+2
source

I was inspired by the above example using common methods:

 public class FunctionConverter : JsonConverter { public override bool CanConvert(Type objectType) { return (objectType == typeof(String)); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { serializer.Serialize(writer, String.Concat("<Function>", value, "</Function>")); } } public static class JsonObjectExtensions { public static String CleanJson(this String s) { return s.Replace("\"<Function>", "").Replace("</Function>\"", ""); } } public partial class JsonObject { public String ToJson() { return JsonConvert.SerializeObject(this).CleanJson(); } } 

And model data:

 public class ConfigModel : JsonObject { [JsonProperty("altFormat", NullValueHandling = NullValueHandling.Ignore)] public String altFormat { get; set; } [JsonProperty("onClose", NullValueHandling = NullValueHandling.Ignore)] [JsonConverter(typeof(FunctionConverter))] public String onClose { get; set; } } 
0
source

Source: https://habr.com/ru/post/1338477/


All Articles