score:0
the json format cannot represent functions like that (directly). you could encode it as a string and then instantiate the function explicitly in the client, or pass the name of a function that's already present at the client.
score:5
from json format definition here http://json.org/ it is clear that you cannot have functions in json.
define your function elsewhere in application and call it explicitly.
any kind of hack to support functions in json is bad practice as it conflicts with purpose of json as "lightweight data-interchange format". you can't interchange functions as they cannot be understood by anything else except javascript.
score:7
i was trying to accomplish something similar. in my case i was using mvc razor syntax trying to generate a json object with a function passed in using the @<text> syntax.
i was able to get the desired output using the json.net library (using jsonconvert and jraw). http://james.newtonking.com/projects/json/help/html/serializerawjson.htm
example:
public class jsobj
{
public string title { get; set; }
public int upperval { get; set; }
public int lowerval { get; set; }
public object mouseover
{
get
{
// use jraw to set the value of the anonymous function
return new jraw(string.format(@"function(){{ return {0}; }}", upperval - lowerval));
}
}
}
// and then serialize using the jsonconvert class
var obj = new jsobj { title = "test", lowerval = 4, upperval = 10 };
var jsonobj = jsonconvert.serializeobject(obj);
that should get you the json object with the function (instead of the function in a string).
{"title":"test","upperval":10,"lowerval":4,"mouseover":function(){ return 6; }}
post: how to serialize a function to json (using razor @<text>)
Source: stackoverflow.com
Related Query
- Serializing a function as a parameter in json using C#
- Parsing JSON for use with Highcharts using jquery .parseJSON or JSON.parse: how to remove quotes from function calls for formatters?
- Formatting JSON data monthwise for HighCharts using MySQL
- Pass Data String as parameter to a Function for HighCharts
- Color contrast setting on dataLabels style parameter using Highcharts 5 is not working on some colors
- Using PhantomJS to create HighCharts grahps server side for use in PDF creation (PHP) - results in exit code 11 from PHPs exec();
- Uncaught TypeError: undefined is not a function when using highcharts
- how to sort and select top 5 elements in a json array using angular JS
- create a donut chart using highchart using jquery json object
- using angular directive to draw highchart pie chart but when i am using it in success function it is not working
- Passing a JavaScript function from JSON encoded PHP
- Highcharts - Using a function to change the tooltip
- Sending javascript function with JSON is possible?
- How To Call A Function To Make A Graph Using HighCharts in PHP
- How to Populate Highchart using Json Object
- Place Data in Highchart using json and post url
- Using global data in High Charts point click custom function in R Shiny
- JSON data not showing in highstock Candlestick chart using PHP & MYSQL
- Highcharts Solid Gauge Dynamic Update Using JSON
- Highcharts: Using JSON to load xAxis data as 'datetime'?
- Dotnet Highchart Using JSON
- passing json values to highcharts from .net code behind
- Charts using Highcharts with multiple series from JSON
- passing formatting JavaScript code to HighCharts with JSON
- Highcharts Dynamic Drilldown using json
- Plot Highchart multiple series line chart using JSON data
- Setting Min and Max values for more than two Highchart Solid Gauges using function
- Highchart is not getting populated using external json data
- Highcharts display label for pie chart using html table as data source
- Error using Highcharts (>= 8.0.1) function stockChart() in Angular
More Query from same tag
- How to embed a Highcharts chart to jekyll blog post?
- highcharts stacked column weird symbol in a bubble
- How to display monthly data in Highcharts graph?
- HighCharts - problems with tickInterval and tickAmount
- Highcharts: Why text of dataLabels inside column bar are not getting justified and are overflowing
- Weekly PDF generation using PHP and Highcharts
- How to find the width of x-axis plotband widths dynamically in react-jsx-highstock (Highcharts)?
- If statement for line colours
- how to add above each column a different pie chart
- How to change the legend data highcharts
- unable to fetch temperature data in highcharts
- How to Change marker symbol and dataLabel with custom style in Highcharts
- Display an error message when chart doesn't appear
- highcharts dynamic background color issue
- High chart partial rendering
- Does Highcharts redraw every time the dataset is changed?
- Drill down function in highcharts in case of line charts
- Json to Chart using highchart export server with Node js module its throw error
- HighChart xAxis Datetime value
- Data exposure for HighCharts
- Change Highmaps map colour
- Dynamically populated highcharts data and database storing
- Plotting data using highcharts piechart in flask web application
- Slider on barchart
- Sorting a group based on value
- How do I remove a numerical data plot on a bar that I added in a line chart?
- Highcharts legend hover for bar chart?
- Highcharts: heatmaps - incorrect colors on reversed colorAxis
- Using HighStocks with highcharts-ng
- Add or declare two plotOptions in highcharts for Synchronized chart