Re: Maintain Order in JSON Import

2016-09-16 Thread Sannyasin Brahmanathaswami
Never mind.. I found it on the forums

Mark just pasted his script there… no "lib" as such.


On 9/16/16, 6:11 PM, "use-livecode on behalf of Sannyasin Brahmanathaswami" 
 wrote:

Richard Gaskin" wrote:

YAML was designed to provide similar serialization to/from plain text, 
but optimized for human readability/writability.  Mark Wieder has a 
nice 
pair of functions for using it in LC, converting to and from arrays.

BR: where can I get these?



___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Re: Maintain Order in JSON Import

2016-09-16 Thread Sannyasin Brahmanathaswami
Richard Gaskin" wrote:

YAML was designed to provide similar serialization to/from plain text, 
but optimized for human readability/writability.  Mark Wieder has a nice 
pair of functions for using it in LC, converting to and from arrays.

BR: where can I get these?



___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


Re: Maintain Order in JSON Import

2016-09-16 Thread pink
JsonImport and JsonExport are not in the "LiveCode Script" dictionary. You
need to switch to "JSON Library" from the dropdown.

mergJSON, easyJSON and fastJSON all output numeric-keyed arrays as JSON
arrays, if you need JSON arrays then you definitely need one of them (ie -
the stuff within a square brace). For my purposes with CouchDB, the JSON LCB
library is insufficient.

mergJSON's  "pretty" mode is great for displaying a readable JSON, it
alphabetizes keys and indents based on depth

I put together a couple of functions a while back to make a readable JSON, I
haven't updated it in a while. It does not sort keys alphabetically, just
adds whitespace to indent key/value pairs based on depth
https://github.com/madpink/gingerjson/blob/master/gingerjson.lc


if needed:   
FastJSON (still being maintained):  https://github.com/bhall2001/fastjson
EasyJSON (stable, but not updated in several years):  
https://github.com/luxlogica/easyjson




--
View this message in context: 
http://runtime-revolution.278305.n4.nabble.com/Maintain-Order-in-JSON-Import-tp4708343p4708523.html
Sent from the Revolution - User mailing list archive at Nabble.com.

___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


Re: Maintain Order in JSON Import

2016-09-16 Thread Monte Goulding

> On 15 Sep 2016, at 12:22 PM, Roger Eller  wrote:
> 
> Sweet!  Can this external be used in a Windows standalone?

It supports all of LiveCode’s platforms and is dual licensed for community 
users too.

Cheers

Monte
___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Re: Maintain Order in JSON Import

2016-09-15 Thread Richard Gaskin

Sannyasin Brahmanathaswami wrote:

> On 9/14/16, 11:28 AM, Richard Gaskin wrote:
>
> Do you need to round-trip?  Where does the data come from, and
> where is it going?
>
> If any part of that is LC on both ends you can save _much_ time
> for that using an encoded array.

> The use case is cross session data on mobile where we would like to
> be able to examine/change the "array" in a text editor.

Have you considered YAML?

JSON was designed as an optimal solution for serialization in 
environments using the JavaScript interpreter.  While it is a plain text 
format human readability/writability is a by-product, with the primary 
goal being exchange of data with REST services that can be de-serialized 
in a call to JS' eval function.


YAML was designed to provide similar serialization to/from plain text, 
but optimized for human readability/writability.  Mark Wieder has a nice 
pair of functions for using it in LC, converting to and from arrays.



> The only way to save an encoded array would be to insert is a a
> custom property in an LC stack and save to the writeable folder
> on mobile.

You can indeed store any data, text or binary in custom properties.  But 
LiveCode also has built-in options for reading/writing both text and 
binary data in files.


The arrayEncode function converts the collection of pointers and memory 
buckets associated with an array into one reasonably compact binary 
form, as easily handled with any file or network I/O routines as any 
other binary data, like images.


You can store LSON in a custom property, but you can also store it in a 
file by itself:


  put arrayEncode(tArray) into url "binfile:/path/to/file.lson"

To read it back into an array:

  put arrayDecode(url "binfile:/path/to/file.lson") into tArray


Just as JSON is designed hand-in-glove to be optimized for the 
JavaScript interpreter, LSON is the with-the-grain serialization option 
in LC.


Being binary, like the BSON used by MongoDB and others, it's best used 
machine-to-machine, not suitable for direct human editing using a text 
editor.


But being so with-the-grain it's easy to make viewers and editors for 
array data as you need them.


And if you anticipate doing a lot of manual editing you may find YAML 
very convenient to work with, designed like a lightweight outliner that 
just happens to work in any text editor.


Being optimized for LC, LSON is not only robust and convenient but also 
pretty fast and compact.  For example, numeric values are stored in 
their binary form, bypassing conversion to and from text.  With sparse 
delimiters and compact binary forms of numbers, in many cases LSON files 
will take less disk space and network transfer time than equivalent data 
in JSON (and far less than most XML).


Of course the main thing is to use whatever work for your project, and 
if you have JSON running well now there's probably no need to change 
anything.


For myself, I tend to pick among these formats like this:

YAML: When I anticipate needing to read/write them manually.

JSON: When I need to exchange data with other people's REST services,
  or when delivering data to a Web browser or other software
  that uses the JavaScript interpreter (like Node.js).

LSON: When the software reading and writing the data will be
  LiveCode, and not meeting the two previous criteria.

Lately most of my work has LC on the server feeding LC clients, so LSON 
has become my default go-to choice unless I have some specific need 
requiring something else.


I have one REST service for which I'll need to support both LC apps and 
Web browsers as clients, and I'm setting up the API so that the file 
extension in the request determines the output format, e.g.:


  get url "http://domain.com/cgi?query.json

...is parsed to run the output through an arrayToJson function before 
sending it back over the wire, while this:


  get url "http://domain.com/cgi?query.lson

...returns an encoded LiveCode array.

--
 Richard Gaskin
 Fourth World Systems
 Software Design and Development for the Desktop, Mobile, and the Web
 
 ambassa...@fourthworld.comhttp://www.FourthWorld.com

___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


Re: Maintain Order in JSON Import

2016-09-15 Thread J. Landman Gay

On 9/14/16 9:01 PM, Sannyasin Brahmanathaswami wrote:

The only way to save an encoded array would be to insert is a a
custom property in an LC stack and save to the writeable folder on
mobile.


Just for the record, the arrayEncode function writes the array to a 
binary file on disk, not to a stack. The arrayEncode and arrayDecode 
functions were created to allow direct export and import of LC arrays.


--
Jacqueline Landman Gay | jac...@hyperactivesw.com
HyperActive Software   | http://www.hyperactivesw.com

___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


Re: Maintain Order in JSON Import

2016-09-14 Thread Roger Eller
Sweet!  Can this external be used in a Windows standalone?

~Roger

On Sep 14, 2016 10:17 PM, "Monte Goulding"  wrote:

>
> > On 15 Sep 2016, at 12:13 PM, Sannyasin Brahmanathaswami <
> bra...@hindu.org> wrote:
> >
> >"Actually if I just let the jsonExport and jsonImport do the work…
> it's valid json… except that what's on disk is all on a single line."
> >
> > Python to the rescue: this works as a text filter in BBEdit
>
> It’s probably worth pointing out that mergJSON has a pretty print option.
>
> Cheers
>
> Monte
> ___
> use-livecode mailing list
> use-livecode@lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Re: Maintain Order in JSON Import

2016-09-14 Thread Trevor DeVore
On Wednesday, September 14, 2016, Monte Goulding  wrote:
>
>
> Ah, I haven’t used the LCB JSON library myself. Perhaps Peter will chime
> in here.
>
> FYI mergJSON turns a sequentially indexed array 1..N <-> a JSON array [ ].
>

I use mergJSON to export for this very reason. I import with the LCB JSON
library, however. I'm pretty sure I have a good reason for that but I'm
really tired right now and can't remember why I ended up using both
libraries. Maybe it is how text is encoding is handled in LCB.

-- 
Trevor DeVore
___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Re: Maintain Order in JSON Import

2016-09-14 Thread Monte Goulding

> On 15 Sep 2016, at 12:13 PM, Sannyasin Brahmanathaswami  
> wrote:
> 
>"Actually if I just let the jsonExport and jsonImport do the work… it's 
> valid json… except that what's on disk is all on a single line."
> 
> Python to the rescue: this works as a text filter in BBEdit 

It’s probably worth pointing out that mergJSON has a pretty print option.

Cheers

Monte
___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Re: Maintain Order in JSON Import

2016-09-14 Thread Sannyasin Brahmanathaswami
bra...@hindu.org> wrote:

"Actually if I just let the jsonExport and jsonImport do the work… it's 
valid json… except that what's on disk is all on a single line."

Python to the rescue: this works as a text filter in BBEdit 

save to:

~/Library/Application Support/BBEdit/Text Filters/

#!/usr/bin/env python
import fileinput
import json
if __name__ == "__main__":
  jsonStr = ''
  for a_line in fileinput.input():
jsonStr = jsonStr + ' ' + a_line.strip()
  jsonObj = json.loads(jsonStr)
  print json.dumps(jsonObj, sort_keys=True, indent=2)

___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Re: Maintain Order in JSON Import

2016-09-14 Thread Sannyasin Brahmanathaswami
The use case is cross session data on mobile where we would like to be able to 
examine/change the "array" in a text editor.

if the user shuts down the app, you must save the data somewhere, and reload to 
gUserSettings  on start up. So, yes, agreed, there are many ways to accomplish 
this. The only way to save an encoded array would be to insert is a a custom 
property in an LC stack and save to the writeable folder on mobile.

disclaimer: this is all a bit new to me so "best architecture" for me is "most 
transparent" architecture. And since we have done and will continue to do a lot 
of API calls to and from the web server, where a lean text only data format 
(JSON)  is useful.. I'm look at using JSON for this kind of thing… so this 
"kinda" goes along with the whole externalization of code to text documents 
(scripts, stacks, libs, behaviors) that are all readable in a text editor… 
having JSON also as the container means we have this model working there too. 
So I can change a value in a user setting (in a text editor) and then in the 
app see that my calls are actually working.

I've got it working now… no issues really, other than my own learning curve and 
being sure not to break any json by hand.  

Actually if I just let the jsonExport and jsonImport do the work… it's valid 
json… except that what's on disk is all on a single line.






 

On 9/14/16, 11:28 AM, "use-livecode on behalf of Richard Gaskin" 
 
wrote:

Do you need to round-trip?  Where does the data come from, and where is 
it going?

If any part of that is LC on both ends you can save _much_ time for that 
using an encoded array.

___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Re: Maintain Order in JSON Import

2016-09-14 Thread Monte Goulding

> On 15 Sep 2016, at 7:18 AM, Sannyasin Brahmanathaswami  
> wrote:
> 
> FYI, further tests with jsonImport/Export round trips, reading and writing:
> 
> jsonImport 
> jsonExport
> 
> are 
> 
> a) not in the dictionary for 
> b) round trip does not support any formatting (no indentation or vertical 
> white space)
> c) square braces not supported on export.

Ah, I haven’t used the LCB JSON library myself. Perhaps Peter will chime in 
here.

FYI mergJSON turns a sequentially indexed array 1..N <-> a JSON array [ ].

Cheers

Monte 
___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Re: Maintain Order in JSON Import

2016-09-14 Thread Richard Gaskin

Sannyasin Brahmanathaswami wrote:

> FYI, further tests with jsonImport/Export round trips, reading and 
writing:

>
> jsonImport
> jsonExport
>
> are
>
> a) not in the dictionary for
> b) round trip does not support any formatting (no indentation or
> vertical white space)
> c) square braces not supported on export.

Do you need to round-trip?  Where does the data come from, and where is 
it going?


If any part of that is LC on both ends you can save _much_ time for that 
using an encoded array.


--
 Richard Gaskin
 Fourth World Systems
 Software Design and Development for the Desktop, Mobile, and the Web
 
 ambassa...@fourthworld.comhttp://www.FourthWorld.com

___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


Re: Maintain Order in JSON Import

2016-09-14 Thread Sannyasin Brahmanathaswami
Monte Goulding wrote:

try

"colorwheel": [
{
   “name”: “Red”,
“color”: “255,0,0"
},


FYI, further tests with jsonImport/Export round trips, reading and writing:

jsonImport 
jsonExport

are 

a) not in the dictionary for 
b) round trip does not support any formatting (no indentation or vertical white 
space)
c) square braces not supported on export.

Valid JSON going in:


{
"settings": {
"global": "",

"modules":

{
"colormeds": 
{"breathCount": "1",
"cycle": "1",
"currentLevel": "1",
"breathRate": "1"
}
}
}
}



gets written out as:  a single line

{"settings": {"global": "","modules": {"colormeds": {"1": {"breathCount": 
"1","cycle": "1","currentLevel": "1","breathRate": "1"}

AND

Square brace notation is not supported, but converted to explicit numeric keys
--not a big problem, but good to be aware that

this goes in:   

{
"settings": {
"global": "",

"modules":

{
"colormeds": 
[
{
"breathCount": "1",
"cycle": "1",
"currentLevel": "1",
"breathRate": "1"
}
]
}
}
}

and comes out as

{"settings": {"global": "","modules": {"colormeds": {"1": {"breathCount": 
"1","cycle": "1","currentLevel": "1","breathRate": "1"}

which, formatted looks like:

{
"settings": {
"global": "",
"modules": {
"colormeds": {
"1": {
"breathCount": "1",
"cycle": "1",
"currentLevel": "1",
"breathRate": "1"
}
}
}
}
}

Actually I find it easier to wrap my brain around the latter… though it would 
be nice if the output were formatted.  Does anyone have a JSON formatter 
function I can pass this thru before writing to disk?

BR
 

___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Re: Maintain Order in JSON Import

2016-09-12 Thread Sannyasin Brahmanathaswami
OK… so long as we are using bracket form… 

 someArray [

]
then we will get numeric, sortable keys… so than just go ahead and use standard 
JSON notation inside the array..

got it. tks

On 9/12/16, 11:31 AM, "use-livecode on behalf of Monte Goulding" 
 wrote:

It is not hacky. It is the way you represent sequences in JSON. The hacky 
part is the pipe delimiter. Try:


"colorwheel": [
{
   “name”: “Red”,
“color”: “255,0,0"
},
...

or 

"colorwheel": [
{
“Red”: “255,0,0"
},


___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Re: Maintain Order in JSON Import

2016-09-12 Thread Monte Goulding

> On 13 Sep 2016, at 7:23 AM, Sannyasin Brahmanathaswami  
> wrote:
> 
> this seems "hacky" but can now order the keys because they default to numeric 
> index

It is not hacky. It is the way you represent sequences in JSON. The hacky part 
is the pipe delimiter. Try:


"colorwheel": [
{
   “name”: “Red”,
“color”: “255,0,0"
},
...

or 

"colorwheel": [
{
“Red”: “255,0,0"
},
...
___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Maintain Order in JSON Import

2016-09-12 Thread Sannyasin Brahmanathaswami
If you import JSON like this:

{
  "colormeds": {
   "progress": {
 "level": "1",
 "cycle": "1"
   },
   "colorwheel": {
 "Red": "255,0,0",
 "Red-Orange": "255,64,0",
 "Orange": "255,127,0",
 "Orange-Yellow": "255,191,0",
 "Yellow": "255,255,0",
 "Yellow-Green": "191,255,0",
 "Green": "0,255,0",
 "Green-Blue": "0,191,255",
 "Blue": "0,0,255",
 "Blue-Violet": "127,0,255",
 "Violet": "127,0,127",
 "Red-Violet": "191,0,127"
  }
}

the keys for:

sConfigColor["colormeds"]["colorwheel"]  are returned as expected in a hashed 
order


Blue

Orange-Yellow

Violet

Green

Green-Blue

Red-Violet

Blue-Violet

Red

Yellow

Red-Orange

Orange

Yellow-Green


is there a simple method for restoring the sequential order as entered in the 
JSON? or a way to store in the JSON that facilitates that requirement?

I solved it by put the values into an array, with a extra delimited, but this 
seems "hacky" but can now order the keys because they default to numeric index

"colorwheel": [
 "Red|255,0,0",
 "Red-Orange|255,64,0",
 "Orange|255,127,0",
 "Orange-Yellow|255,191,0",
 "Yellow|255,255,0",
 "Yellow-Green|191,255,0",
 "Green|0,255,0",
 "Green-Blue|0,191,255",
 "Blue|0,0,255",
 "Blue-Violet|127,0,255",
 "Violet|127,0,127",
 "Red-Violet|191,0,127"]
   },

Now I have to set the delimiter to pipe and then fetch the item…
Is there a better way?



___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode