Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jackson parsing javascript function

Is it possible to parse a JSON string containing a Javascript function into a Jackson JsonNode ?

The (JSON formatted) string I try to parse :

{
    "key1" : "value1",
    "key2" : "value2",
    "key3" : function () {
       // some javascript code
    }
}  

In a Java object, I would expect something like a Map (Javascript function converted to String).

Currently, I have the following exception :

org.codehaus.jackson.JsonParseException: Unrecognized token 'function'

I found lots of features for non-valid JSON use but still not what I would like ...

Any idea ?

Thanks!

like image 276
tduchateau Avatar asked Apr 29 '26 15:04

tduchateau


1 Answers

JSON doesn't allow functions. It is meant for safe data-transfer though you could encode the function as a string like this:

{
    "key1" : "value1",
    "key2" : "value2",
    "key3" : "function () { ... }"
}

...but upon re-encoding, it would be a string rather than a function unless you were to eval() it (though that could well be unsafe).

like image 91
Brett Zamir Avatar answered May 01 '26 06:05

Brett Zamir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!