JavaScript interop, but how ?

80 views
Skip to first unread message

Roger Gilliar

unread,
Nov 29, 2016, 1:38:43 PM11/29/16
to ceylon-users
I'm still having troubles to figure the right way to use JSON objects in Ceylon. For example how do I iterate over all properties ? How do I check if a property exists ? How to convert a JSON object to something more Ceylon like a stream of pairs ?

Enrique Zamudio

unread,
Nov 30, 2016, 12:40:26 PM11/30/16
to ceylon-users
you can still call the methods in the JS Object class, like \iObject.keys(x) or x.hasOwnProperty("foo") for example.

Iterating over an object's properties is a little more cumbersome since there's no way to generate a for(p in o) loop, so in Ceylon you need to do something like this:

dynamic props = \iObject.keys(x);
for (String s : props) {
  print("``s`` -> ``x[s]``");

Gavin King

unread,
Nov 30, 2016, 12:53:48 PM11/30/16
to ceylon...@googlegroups.com
Here's an example in the Web IDE.

http://try.ceylon-lang.org/?gist=eb467e4cf7f812861d81a080d7fbb249
> --
> You received this message because you are subscribed to the Google Groups
> "ceylon-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ceylon-users...@googlegroups.com.
> To post to this group, send email to ceylon...@googlegroups.com.
> Visit this group at https://groups.google.com/group/ceylon-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ceylon-users/1577b3ff-d22e-4307-b56d-ae544e408efb%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Gavin King
ga...@ceylon-lang.org
http://profiles.google.com/gavin.king
http://ceylon-lang.org
http://hibernate.org
http://seamframework.org

Roger Gilliar

unread,
Nov 30, 2016, 1:46:32 PM11/30/16
to ceylon-users
Thanks, I'm using underscore now and that works quite good. One thing I do not understand is the meaning of Anything in a dynamic block. Assigning v1 and v2 works but v3 gives TypeError: Expected ceylon.language::Anything (Record.ceylon 20:26-20:31)

 
value fields = HashMap<String, String>();

dynamic {
     obj
= data
     
for (k in _keys(obj)) {
         
String v1 = k;
         v2
= obj[k]
         
Anything v3 = obj[k];
         fields
.put(k.lowercased, k)
     
}
 
}

Gavin King

unread,
Dec 1, 2016, 3:20:55 AM12/1/16
to ceylon...@googlegroups.com
Right, a plain JavaScript object isn't an instance of any Ceylon
class, nor even of the class Object, until you assign it to a dynamic
interface type. This is discussed in the tour here:

https://ceylon-lang.org/documentation/1.3/tour/dynamic/#dynamic_instantiation_expressions

So this code results in an error, since point doesn't even have the
operations of Ceylon's Object type:

dynamic {
dynamic point = dynamic [ x=1.0; y=2.0; ];
Anything any = point;
}

However, the following code is OK:

dynamic Point {
shared formal Float x;
shared formal Float y;
}

dynamic {
dynamic point = dynamic [ x=1.0; y=2.0; ];
Point typedPoint = point;
Anything any = typedPoint;
}

It's acceptable because the assignment of point to Point "dresses" the
JavaScript object as a Ceylon object of type Point.
> --
> You received this message because you are subscribed to the Google Groups
> "ceylon-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ceylon-users...@googlegroups.com.
> To post to this group, send email to ceylon...@googlegroups.com.
> Visit this group at https://groups.google.com/group/ceylon-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ceylon-users/7622b65d-ad1f-420c-8da0-6a4b0e964a71%40googlegroups.com.

Stephane Epardaud

unread,
Dec 1, 2016, 4:26:02 AM12/1/16
to ceylon...@googlegroups.com
You're missing a few semicolons at the end of your lines that's why, I think.

--
You received this message because you are subscribed to the Google Groups "ceylon-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceylon-users+unsubscribe@googlegroups.com.

To post to this group, send email to ceylon...@googlegroups.com.
Visit this group at https://groups.google.com/group/ceylon-users.

For more options, visit https://groups.google.com/d/optout.



--
Stéphane Épardaud

Roger Gilliar

unread,
Dec 1, 2016, 3:07:51 PM12/1/16
to ceylon-users
I changed the code a bit. The real problem seems to be, that a Date value can' be assigned to Anything. Is therefore a Date object considered a plain JS object ?

value fields = HashMap<String,Anything>();
value ignore
= ["$$", "getT$name", "getT$all", "string", "$prop$getString", "hash", "$prop$getHash"];


   
dynamic {
        obj
= data;
       
for (k in _keys(obj)) {

           
if (!ignore.contains(k)) {
               
dynamic v = obj[k];
               
try {
                    fields
.put(k.lowercased, v);
               
} catch (Throwable t) {
                   
print("-----");
                   
print(k); // gives timestamp1
                   
print(v); // gives Tue Oct 04 2016 16:16:55 GMT+0200 (CEST)
                   
print(_isDate(v)); // gives true
               
}
           
}
       
}
   
}



Gavin King

unread,
Dec 1, 2016, 3:55:50 PM12/1/16
to ceylon...@googlegroups.com
Right, yes, a JavaScript Date isn't an instance of any Ceylon class.

Here's how to convert a JavaScript Data to a ceylon.time DateTime object:

http://try.ceylon-lang.org/?gist=6a00f7c56bcd2db586ba543a18ff9ebd
> --
> You received this message because you are subscribed to the Google Groups
> "ceylon-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ceylon-users...@googlegroups.com.
> To post to this group, send email to ceylon...@googlegroups.com.
> Visit this group at https://groups.google.com/group/ceylon-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ceylon-users/fb35ebde-65a9-44b4-8cd7-2e5e9e208926%40googlegroups.com.

Roger Gilliar

unread,
Dec 3, 2016, 1:35:33 PM12/3/16
to ceylon-users
Just out of curiosity: Why is a Date not an instance of a Ceylon class, but a JS string is an instance of some Ceylon class ?

Gavin King

unread,
Dec 3, 2016, 3:57:42 PM12/3/16
to ceylon...@googlegroups.com
Well, String is defined in the Ceylon language module and the compiler
and runtime do a bunch of special magic to make JavaScript strings
appear to be instances of String. But there's no Date type in the
Ceylon language module to do any sort of equivalent mapping.

On Sat, Dec 3, 2016 at 7:35 PM, 'Roger Gilliar' via ceylon-users
> https://groups.google.com/d/msgid/ceylon-users/8c8faea3-89f5-4857-a203-ec4be12d5397%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages