Thanks, issue https://github.com/orientechnologies/orientdb/issues/2390 created
Op dinsdag 27 mei 2014 11:36:58 UTC+2 schreef Artem Orobets: > > Hi, > > To serialize objects into JSON in a similar manner you can use following > method. > *com.orientechnologies.orient.core.serialization.serializer.record.string.ORecordSerializerStringAbstract#toString(com.orientechnologies.orient.core.record.ORecordInternal<?>, > > java.lang.String)* > To get an instance of serializer you can use this constant > *ORecordSerializerJSON.INSTANCE* > > But this rather a workaround for you. > > As for automatical wrapping *all* Documents in parameters to be used as > JS objects: > > 1. We should left the possibility to use ODocument specific methods. > This might be solved by add a special method (e.g. o.underlying()) that > will return an original object. > 2. We should make a deep wrapping, because a document can contain > another document as a field > 3. It is not clear how to support a backward compatibility > > > However provide a helper function to convert ODocuments and results sets > to js object might be very useful. We can use it not only to convert > parameters but also to make handling of query result easier in JS. > Could you create a ticket as for creation such helper function? > > > Best regards, > Artem Orobets > > * Orient Technologiesthe Company behind OrientDB* > > > 2014-05-27 11:05 GMT+03:00 MrFT <[email protected] <javascript:>>: > >> Hello, >> >> I have been testing a bit more with the syntax you proposed. >> My final goal was to be able to call a function insertOrUpdate( >> json-object ) that would help support a json webservice. >> >> So I tried your propsal: >> >> *select jsonParameterTest( $j ) LET $j = (select 1 as a, 'zE' as b)* >> >> And that works syntactically. >> I also tried some other ways such as >> >> *select jsonParameterTest( map( "a", 1, "b", 'zE' ) )* >> >> *select jsonParameterTest( document( "a", 1, "b", 'zE' ) )* >> >> and they all work, but the 'problem' is. >> As soon as you are in javascript I would have expected to be working with >> these parameters as javascript maps or lists, but instead, they are Java >> classes. (=> typeof par == 'object') >> Moreover, they all have a different class. >> >> >> - in the first example the par.getclass() will return >> *class com.orientechnologies.orient.core.sql.query.OResultSet* >> (you can cal the iterator that returns ODocuments) >> - in the second exmple the par.getclass() will return >> *class java.util.HashMap* >> - in the third exmple the par.getclass() will return >> *class com.orientechnologies.orient.core.record.impl.ODocument* >> >> QUESTIONS: >> >> - would it be an idea to wrap these as javascript lists and maps, >> when they are passed to a javascript function, because that's easier to >> work with from javascript? >> - or alternatively: is there a function that can be called to >> transform these (regardless whether it's a HashMap, an ODocument or an >> OResultSet) either to javascript objects directly or that transforms them >> into json, so I can use JSON.parse( jsonString ) >> *[ODocument has a method toJSON, but HashMaps and ResultSets haven't]* >> >> *What do you think?* >> >> >> >> Op vrijdag 9 mei 2014 17:19:06 UTC+2 schreef Lvc@: >> >>> Hi, >>> That syntax is cool, please can you open a new issue to support it? >>> >>> In the meanwhile as workaround, with 1.7-SNAPSHOT you could do: >>> >>> >>> *select jsonParameterTest( $j ) LET $j = (select 1 as a, 'zE' as b) >>> FETCHPLAN *:1 * >>> >>> Lvc@ >>> >>> >>> >>> Lvc@ >>> >>> >>> On 6 May 2014 10:35, MrFT <[email protected]> wrote: >>> >>>> Hello, >>>> >>>> from what I get so far, the parameters for a server-side javascript >>>> function can be simple things like strings and numbers. >>>> >>>> My question is: is it possible to have a json object as the parameter? >>>> >>>> For example, when calling the function >>>> >>>> function jsonparameterTest( par ) >>>> if ( typeof par == 'object' ) { >>>> return JSON.stringify( par ); >>>> } >>>> else { >>>> return "NO OBJECT: " + par; >>>> } >>>> } >>>> >>>> in the studio like this: >>>> >>>> select jsonParameterTest( 'mystringparameter' ) FETCHPLAN *:1 >>>> >>>> It works as expected. >>>> >>>> But when I try (and that's what I would like to do, if possible): >>>> select jsonParameterTest( { "a": 1, "b": "zE" } ) FETCHPLAN *:1 >>>> >>>> I get: >>>> com.orientechnologies.orient.core.command.script.OCommandScriptException: >>>> Error on parsing script at position #0: Error on execution of the script >>>> Script: jsonParameterTest ------^ --> javax.script.ScriptException: >>>> sun.org.mozilla.javascript.internal.EvaluatorException: Java class >>>> "[Ljava.lang.annotation.Annotation;" has no public instance field or >>>> method named "toJSON". (#93) in at line number 93 --> >>>> sun.org.mozilla.javascript.internal.EvaluatorException: Java class >>>> "[Ljava.lang.annotation.Annotation;" has no public instance field or >>>> method named "toJSON". (#93) >>>> >>>> So my question is: >>>> >>>> - can this be done, and >>>> - if so, what am I doing wrong? >>>> >>>> >>>> -- >>>> >>>> --- >>>> You received this message because you are subscribed to the Google >>>> Groups "OrientDB" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to [email protected]. >>>> >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>> >>> -- >> >> --- >> You received this message because you are subscribed to the Google Groups >> "OrientDB" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected] <javascript:>. >> For more options, visit https://groups.google.com/d/optout. >> > > -- --- You received this message because you are subscribed to the Google Groups "OrientDB" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
