Question about the new JSON ⌶ beam

General APL language issues

Question about the new JSON ⌶ beam

Postby PGilbert on Wed Apr 22, 2015 12:51 am

While using the JSON library included in the APLWiki if you do:
      #.UTIL.JSON.fromAPL ⍳5
[1,5,1,2,3,4,5]
#.UTIL.JSON.fromAPL 3 2⍴⍳6
[2,3,2,1,2,3,4,5,6]


When you do the same with the new ⌶ beam:
      (7160⌶)⍳5
[1,2,3,4,5]
(7160⌶)3 2⍴⍳6
DOMAIN ERROR: Array unsupported by JSON
(7160⌶)3 2⍴⍳6


The ⍳5 representation is not the same and there is an error while doing an array. What is it that I am doing wrong ?

Thanks in advance.

Pierre Gilbert
User avatar
PGilbert
 
Posts: 419
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: Question about the new JSON ⌶ beam

Postby Phil Last on Wed Apr 22, 2015 7:00 am

The JSON class in the wiki has an enhancement that allows APL to APL transfer of all kinds of arrays. If you read it into another language your 2 3⍴⍳6 will turn into the 9-vector 2 2 3 1 2 3 4 5 6 because it contains rank and shape information.

If you wrote ⍳6 using the i-beam then read it into Dyalog with the class it would convert into the vector
      {((1⍴⍵)⍴1↓⍵)⍴(1+1⍴⍵)↓⍵}1 2 3 4 5 6
3 4

JSON is incapable natively to represent other than lists and objects. Multi-D arrays do not qualify.

The i-beam has not been enhanced in such a way and it would be foolhardy of Dyalog to do so.

What we really need is a native facility to serialise APL arrays and to represent them simply in code.
User avatar
Phil Last
 
Posts: 576
Joined: Thu Jun 18, 2009 6:29 pm

Re: Question about the new JSON ⌶ beam

Postby PGilbert on Wed Apr 22, 2015 11:29 am

Thanks Phil for your answer this is a surprise for me, the JSON of the Wiki is doing all that we want and were hoping that the new I beam will do the same but with more speed. We are looking at a way to serialize the Apl variable into a text file with JSON so that it could be read and deserialize by a C# program. This is in order to have a way to exchange data with the other programming language. I suppose it will not be JSON since it cannot accept an array.

Is what was done by David Liebtag http://www.davidliebtag.com/v2f.html of any help for us to have a way to represent Apl data in a function ?
User avatar
PGilbert
 
Posts: 419
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: Question about the new JSON ⌶ beam

Postby Richard|Dyalog on Wed Apr 22, 2015 4:35 pm

The I-Beams import and export standard JSON and are compatible with all other standard JSON producers and consumers - and, as a result, import and export only data which can be described by standard JSON. By using additional data to carry shape, the utility you cite allows additional data formats to be transmitted, but is no longer using JSON - it will only work with non-standard parsers which agree the new format.

I can see no reason why Dyalog should not extend the I-Beams in future to optionally implement such extensions too. I am not keen on the way JSON has been extended in the examples you gave because there is no indication that there is extended JSON in it: "standard" JSON parsers would accept and interpret the data differently. Perhaps a better extension would have been to represent ⍳5 as [1,5:1,2,3,4,5] which a standard parser would reject, for example.

If you want to use the new I-Beams in place of the utility and have purely numeric data you may find

      xj←{(⍴⍴⍵),(⍴⍵),,⍵}


useful:

      0(7160⌶) xj ⍳5
[1,5,1,2,3,4,5]
0(7160⌶) xj 3 2⍴⍳6
[2,3,2,1,2,3,4,5,6]
User avatar
Richard|Dyalog
 
Posts: 42
Joined: Thu Oct 02, 2008 11:11 am

Re: Question about the new JSON ⌶ beam

Postby Phil Last on Wed Apr 22, 2015 4:50 pm

I was thinking along the same lines when I saw Richard's reply.

If you were expecting your C# program to understand multi-d arrays then emulating the additional stuff the JSON class does, as Richard suggests, or using some other protocol that is understood at both ends and passing it to the i-beam ought to be all you need.
User avatar
Phil Last
 
Posts: 576
Joined: Thu Jun 18, 2009 6:29 pm

Re: Question about the new JSON ⌶ beam

Postby PGilbert on Wed Apr 22, 2015 8:48 pm

The following is working for me for numeric arrays:

Code: Select all
 xj←{
     0=↑1↑0⍴⍵:0(7160⌶)(⍴⍴⍵),(⍴⍵),,⍵    ⍝ Numeric Apl to JSON
     {((1⍴⍵)⍴1↓⍵)⍴(1+1⍴⍵)↓⍵}0(7159⌶)⍵  ⍝ JSON to Numeric Apl
 }

      xj ⍳5
[1,5,1,2,3,4,5]
      xj 3 2⍴⍳6
[2,3,2,1,2,3,4,5,6]

      xj '[2,3,2,1,2,3,4,5,6]'
1 2
3 4
5 6
      xj '[1,5,1,2,3,4,5]'
1 2 3 4 5


Thanks Phil and Richard this is answering my question. I was hoping that the APLer could serialize its data to JSON and the C# user could use a standard dll like http://www.newtonsoft.com/json to read back the APL data into the C# (.Net) world but looks like there is some limitation in the JSON definition for arrays.

I was hoping also that we could serialize any APL data to JSON, pass it to the .Net world as characters and have a dll (a converter like http://jsonclassgenerator.codeplex.com/) on the other side to deserialize it and use it for DataBinding (and vice versa), that does not seems easily possible now.

Thanks again everyone.
Last edited by PGilbert on Thu Apr 23, 2015 11:19 am, edited 1 time in total.
User avatar
PGilbert
 
Posts: 419
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: Question about the new JSON ⌶ beam

Postby Vince|Dyalog on Thu Apr 23, 2015 8:08 am

Hi Pierre,

Thanks for your thanks, but...

it was my colleague Richard who replied to you and Phil :)

Regards,

Vince
Vince|Dyalog
 
Posts: 355
Joined: Wed Oct 01, 2008 9:39 am

Re: Question about the new JSON ⌶ beam

Postby PGilbert on Thu Apr 23, 2015 11:19 am

Thanks Vince, I corrected my previous post.
User avatar
PGilbert
 
Posts: 419
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: Question about the new JSON ⌶ beam

Postby Phil Last on Thu Apr 23, 2015 12:17 pm

Sorry being off topic but it's no more so than the previous posts by Vince and Pierre.

If you're reading this please look back at the previous three posts in chronological order starting with that in which Pierre appears to thank Richard and me.

What you'll find is that Vince's mail no longer makes any sense. And you'll find the reason in the next one, the one before this where Pierre admits to having changed his previous one.

This is only my personal preference but could we please have a moratorium on people changing their mails after someone else has already replied to them.

It was OK for Ingsoc but is not helpful here.

Vince's mail said enough.
User avatar
Phil Last
 
Posts: 576
Joined: Thu Jun 18, 2009 6:29 pm

Re: Question about the new JSON ⌶ beam

Postby DanB|Dyalog on Thu Apr 23, 2015 12:42 pm

There is a JSON namespace in SALT that has options to allow you to include or exclude quotes around names and include or exclude rank and shape of data.
JSON.fromAPL has these 2 options and JSON.toAPL has the option to rebuild the object using the shape data.
For example:
Code: Select all
      ]load tools/inet/json
#.JSON
      JSON.fromAPL ⍳5
[1,2,3,4,5]
      0 1 JSON.fromAPL ⍳5
[1,5,1,2,3,4,5]
      0 1 JSON.fromAPL ⍳2 3
[2,2,3,[1,2,1,1],[1,2,1,2],[1,2,1,3],[1,2,2,1],[1,2,2,2],[1,2,2,3]]
      1 JSON.toAPL 0 1 JSON.fromAPL ⍳2 3
 1 1  1 2  1 3
 2 1  2 2  2 3

      'N'⎕ns'' ⋄ N.a←'abc' ⋄ N.n←⍳3
      JSON.fromAPL N
{a:"abc",n:[1,2,3]}
      1 JSON.fromAPL N
{"a":"abc","n":[1,2,3]}
      1 1 JSON.fromAPL N
{"a":[1,3,"abc"],"n":[1,3,1,2,3]}

Invalid names are coded as with the new Ibeam:
Code: Select all
      j←'{"a":[1,3,"abc"],"2n":[1,3,1,2,3]}' ⍝ invalid name
      n←JSON.toAPL j
      n.⎕nl ¯2
 a  ⍙_50_110
DanB|Dyalog
 

Next

Return to Language

Who is online

Users browsing this forum: Bing [Bot] and 1 guest