1

I know how to create members and values in a QJsonObject. What I want to do is create a Json function that when called will call the assigned function.

Is this possible?

SPlatten
  • 5,334
  • 11
  • 57
  • 128
  • 2
    JSON is **not JavaScript** and does not have “functions”. JSON could be used to store a *string* representing a (JavaScript or whatever) “function”.. – user2864740 Mar 16 '19 at 20:23
  • Yes it does in JavaScript which is where JSON came from. – SPlatten Mar 16 '19 at 20:24
  • 1
    That is an incorrect conclusion. Please visit http://json.org so that you can start spreading correct, validated, information. Aside from some similarities with JS Object syntax, JSON is not JavaScript/ECMAScript. – user2864740 Mar 16 '19 at 20:25
  • What’s the actual use-case? There are ways to embed a JS engine. Or maybe it not needed anyway.. also, consider that the goal might be to generate JavaScript *instead of* JSON. – user2864740 Mar 16 '19 at 20:31
  • According to the specification JSON can contain objects. An object is not just a collection of data types, an object contains methods. – SPlatten Mar 16 '19 at 20:32
  • FYI @SPlatten, context of words is important. I’m done ‘debating’ a Kelly. – user2864740 Mar 16 '19 at 20:33

1 Answers1

1

I think that is not possible, functions are not a valid json value.

Valid json values are : string, number, object, array, boolean or null.

In Qt, valid QJsonValues are: bool QJsonValue::Bool, QJsonValue::Double, QJsonValue::String, QJsonValue::Array, QJsonValue::Object, QJsonValue::Null

json spec

Community
  • 1
  • 1
jgoday
  • 2,768
  • 1
  • 18
  • 17
  • Thank you, I’ll wait a bit longer to see if anyone has any suggestions. It’s a bit of a hole in the implementation of that is really the case. – SPlatten Mar 16 '19 at 20:22
  • 2
    And, storing a function in a ' Data Interchange Format' it isn't a good idea – jgoday Mar 16 '19 at 20:22
  • ,it’s depends on why I want it... I’m developing an engine that will partner objects with scripting. – SPlatten Mar 16 '19 at 20:23
  • Ok, it is like a qt method (Q_INVOKABLE) called from the javascript engine or something like that ? – jgoday Mar 16 '19 at 20:25
  • Yes, I want to return a QJsonObject back to my scrip which works, I want to assign a function to the JSON which invokes a C++ method. – SPlatten Mar 16 '19 at 20:29
  • But, you can share an existing QObject to the script engine, and all the properties and public slots will be accesible inside javascript. Is not that good for your problem? – jgoday Mar 16 '19 at 20:31
  • That’s what I’m try to do, question is how? – SPlatten Mar 16 '19 at 20:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/190172/discussion-between-jgoday-and-splatten). – jgoday Mar 16 '19 at 20:35
  • Thank you for your time and help, I think I'm on the way to sorting it out now. – SPlatten Mar 16 '19 at 21:21