David,
First off I hadn't seen this when I posted on the other thread so thanks
for giving me something specific.

On Wed, Jul 26, 2017 at 10:24 AM, David Adams via 4D_Tech <
4d_tech@lists.4d.com> wrote:

> Anyway, to your question. Check out JSON libraries in any other language
> you like, you'll see what I'm saying. You can
>
> * Freely inject any valid JSON element at any point, so arrays within
> objects, objects within arrays, whatever you need.
> * Read the same.
>
​I banged this out pretty quickly:
​

$obj:=JSON Parse("{}")

OB SET($obj;"name";"kirk")

ARRAY OBJECT($aObjs;5)

​ // would be nice to be able to specify a default value...​

$aObjs{1}:=JSON Parse("{}")
$aObjs{2}:=JSON Parse("{}")
$aObjs{3}:=JSON Parse("{}")
$aObjs{4}:=JSON Parse("{}")
$aObjs{5}:=JSON Parse("{}")

OB SET ARRAY($obj;"array1";$aObjs)

ARRAY LONGINT($aLong;3)
$aLong{1}:=1000
$aLong{2}:=2000
$aLong{3}:=3000

OB SET ARRAY($aObjs{1};"longs";$aLong)
OB SET($aObjs{1};"timeStamp";String(Current date;ISO date;Current time))

OB SET($aObjs{2};"key1";"a")

$obj2:=JSON Parse("{}")
OB SET($obj2;\
"key1";"asdfas";\
"key2";"asdfas";\
"key3";"asdfas")

OB SET($obj;"obj2-1";OB Copy($obj2))

OB SET($obj2;"key2";"I changed this one")

OB SET($aObjs{5};"obj2-2";$obj2)

4D_ALERT (JSON Stringify($obj;*))


​and the object it creates is:

​{
"name": "kirk",
"array1": [
{
"longs": [
1000,
2000,
3000
],
"timeStamp": "2017-07-26T12:29:01"
},
{
"key1": "a"
},
{},
{},
{
"obj2-2": {
"key1": "asdfas",
"key2": "I changed this one",
"key3": "asdfas"
}
}
],
"obj2-1": {
"key1": "asdfas",
"key2": "asdfas",
"key3": "asdfas"
}
}


You've got a point about mixed arrays. 4D doesn't deal with them well. I
couldn't get 4D to let me change longs[2] to an object, for instance,
without loosing the longint values.

Though it will read such a JSON - I made the change in a text editor, saved
the file and 4D parses it, correctly, to a c-obj. This is with 15.4HF3. But
it won't let me manipulate it fully. Suggests that JSON is mapped fully
internally but the tools aren't fully written.

I tried the same thing with NTK and I can, indeed, drill down into the
array and manipulate the elements independently of each other.

And I mis-spoke about not being able to change a specific key and have it
propagate up to the parent object. I'm not sure what mistake I made before
that lead me to that conclusion but I see I was wrong. Or maybe it's
because I was working in v13? Anyway, I stand corrected about NTK in v15+.

* Scan items and figure out a bit about their type. (This is a real pain in
> 4D when you use their array embedding tools.)
>
​This really gets to be program specific. If you go back to the json.org
it's defined an a text based data exchange format. So there's nothing in
the json spec about data type - it's all text. All the 'type' stuff has to
be added on to whatever the program wants to do. With the exception of
mixed arrays 4D is pretty good about managing the it's data types within a
c-obj. If I put a date value into a key I can get a date back out and I can
find out it's a data value. ​

Here's an exercise: Define a C_OBJECT that has 'rules'. These rules can
> just be in your head, but they should be something like "This element which
> is required must have a value in the range such and so. This element which
> is an array must have elements with contents in this range", etc. Figure
> out how to do it in 4D. Mostly, you can, some bits, you just can't. I
> tried. Hard. Failed.
>

​It sounds like you are working on a project where you want to be able to
import a data object from a json and be able to completely organize the
database based on the data found. That would be cool. But there's going to
have to be some negotiation beforehand about what's what I think, and the
lack of that isn't a fault on 4D's part. In the javaScript world there's
something called Simple Schema that lets you create and impose rules on
json data for things like Mongo. ​This lets you create the rules like you
mention. I can do the same thing in 4D and then have a criteria to validate
a data object against to see if I know what to do with all.


-- 
Kirk Brooks
San Francisco, CA
=======================

*The only thing necessary for the triumph of evil is for good men to do
nothing.*

*- Edmund Burke*
**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**********************************************************************

Reply via email to