Home   Archive   Permalink



Rebol can't LOAD SAVEd data

I can't find a sensible way to save some data structures that I have. I have isolated the problem below. Any ideas?
    
>> a: make object! reduce [to-set-word to-string 1001 "test"]
>> probe a
make object! [
     1001: "test"
]
>> save %test a
>> b: load %test
** Syntax Error: Invalid time -- 1001:
** Near: (line 2) 1001: "test"


posted by:   Johan Rönnblom       3-Jun-2013/18:18:19-7:00



Rebol 2.x allows you to create words using 'to-word (or similar) that aren't actually valid. It will serialize them, but it will not 'load them.
    
This is not definitive, but I believe this best describes a valid word: http://rgchris.github.io/Rebol-Notation/#Word
    
One other note: not exactly intuitive, but for serialization 'load actually corresponds to 'save/all (or 'mold/all). 'do correlates to 'save (or 'mold).

posted by:   Chris       4-Jun-2013/11:01:45-7:00



Well, the data in this case comes from JSON data and all Rebol JSON loaders that I can find seem to create such objects. It's not really much help if these objects are "invalid" in some way, since there doesn't seem to be any straightforward way to avoid that.
    
What I can do is to re-encode the data to JSON again and save the JSON string. It works, but the JSON<->Rebol parsers are quite slow so it would be much preferable to dump the data in some native format.

posted by:   Johan Rönnblom       4-Jun-2013/11:10:07-7:00



My JSON loader: http://reb4.me/r/altjson (or R3 http://reb4.me/r3/altjson) has a /flat refinement that uses the tag! datatype as a key.
    
     do http://reb4.me/r/altjson
     load-json/flat {{"1001":"Test"}}
    
Additionally, it'll convert blocks of [tag! anytype! ...] form back into a JSON object.
    
If you have any speed issues with AltJSON, let me know—I haven't had any feedback to that effect, but I'm always interested in finding ways to improve it.

posted by:   Chris       4-Jun-2013/23:59:34-7:00



That link didn't go so well, will try again:
    
http://reb4.me/r/altjson

posted by:   Chris       5-Jun-2013/0:00:45-7:00



Thanks, I'll try that and report results in this thread!

posted by:   Johan Rönnblom       5-Jun-2013/4:51:52-7:00



Now I had time to work on this.
    
First, when it comes to speed, it's a little faster than the json.org parser. When I say it's slow it's not meant as a complaint though. I'm sure it's fine for most purposes. But of course it is always going to be slower than saving things in a native format.
    
So I tried the 'flat' option which required me to rework my code a bit, but that was all for the better. This solves my problem.
    
I did notice one issue though, that is not important in my case but I thought I should report it anyway:
    
to-json load-json/flat {{"foo": [], "bar": {}}}
    
Notice that the empty object has been turned into an empty array. This works without the 'flat' refinement or for non-empty objects, which is good enough for me, but might break in other contexts.

posted by:   Johan Rönnblom       9-Jun-2013/6:03:40-7:00



For Rebol 2, that is likely to stay unresolved. For Rebol 3, it's likely I'll switch to the MAP! type to represent JSON Objects as it's a closer match.

posted by:   Chris       11-Jun-2013/4:41:34-7:00



I'm afraid I found some more problems.
    
First, it is using the function "take", which is not supposed to be available in Rebol 2.x. However it seems to be available in some versions and not others, which doesn't exactly help with testing.
    
So when I tried this on Windows (Rebol 1.3.2.3.1) it does not work at all. I then tried to replace the single use of "take" like this:
------
to-parent: [(here: take branch)]
------
to-parent: [(here: first branch)]
remove branch
------
However this does not seem to work, or there are other problems:
    
>> probe load-json/flat {{"fruits": [{"name": "apple"}], "fish": [{"name": "bass"}]}}
    
[
<fruits> [
    [
     <name> "apple"
    ]
    <fish> [
     [
     <name> "bass"
     ]
    ]
]
]
    
As you can see, it has wrongly moved the fish into the fruits.

posted by:   Johan Rönnblom       26-Jul-2013/7:34:08-7:00



I reactivated my brain and changed the fix to this:
    
------
to-parent: [(here: take branch)]
------
to-parent: [(here: first branch remove branch)]
------
    
This seems to work a lot better.

posted by:   Johan Rönnblom       26-Jul-2013/7:52:03-7:00