Author Topic: How to read JSON file format via lisp?  (Read 3997 times)

0 Members and 1 Guest are viewing this topic.

petar_dimov

  • Mosquito
  • Posts: 10
How to read JSON file format via lisp?
« on: October 03, 2016, 04:10:13 AM »
Hello,
I have file with following structure:
  {
   "attributes": {
    "color": ffffaa00,
    "layer": LAY012,
    "areaval": 1351.014,
   },
   "geometry": {
    "rings": [
     [
      [
       5651.534,
       8026.656
      ],
   -----------
      [
       5653.917,
       8083.191
      ],
     ]
    ]
   }
  }
Files structure represents json format and is more then 30k lines.
What is the easy way to read them. I read a lot about MSXML.DOMDocument, but it only support xml format.
Is there something similar but for json files? I want to represent it as list value or something like xml node. How can i split it to (key.value) for example?
Thanks in advice!

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2141
  • class keyThumper<T>:ILazy<T>
Re: How to read JSON file format via lisp?
« Reply #1 on: October 03, 2016, 05:35:38 AM »

I don't have time for a fully researched response, but I recall reading that there are converters available for JSON to XML.

Regards,
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: How to read JSON file format via lisp?
« Reply #2 on: October 03, 2016, 06:40:40 AM »
    "color": ffffaa00,
    "layer": LAY012,
i don't think this is the correct json structure
mustn't it be
Quote
    "color": "ffffaa00",
    "layer": "LAY012",
?

in the simplest cases just like yours there is no need to parse json
Code: [Select]
(setq str "{\"attributes\": {\"color\": \"ffffaa00\",\"layer\": \"LAY012\",\"areaval\": 1351.014,},\"geometry\": {\"rings\": [[[5651.534,8026.656][5653.917,8083.191]]]}}")
(read (vl-string-translate "[]{}:," "()()  " str))

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: How to read JSON file format via lisp?
« Reply #3 on: October 03, 2016, 09:05:03 AM »

Least amount of work in order to get this running "correctly" is probably getting an ActiveX library to call through AutoLisp. Perhaps look at the libraries listed at the botom of this page: http://json.org/


In particular those for VisualBasic. I think the VB-JSON library is intended for VB6 ... so it should be an ActiveX library.


Alternatively rolling one from DotNet and opening such as a lispfunction shouldn't be a huge load of work, definitely less than trying to implement it direct in AutoLisp - especially if you want all the nuances possible in JSon.


This sort of thing is why I'd discourage such formats if at all possible. They'd only be useful if you need the data in some other program which can only read/write JSon / XML. If you simply want to save/send data which you again read/modify/save from another AutoLisp, then everything XML/JSon/ProtoBuf/whatever other serialization can do can be done using normal S-Expressions (i.e. normal Lisp syntax which you can get using the read/write built-in functions).


Actually if you're going to have to re-implement it you're wasting your time. Even if you "have" to do so, I'd avoid XML/JSon (or in fact any human-readable and especially human-modifyable format). That's because most of your time will be spent on trying to catch slight errors someone made while writing it. And if you just use this for communication between programs, then why a human-readable format at all? Much more efficient is any sort of native format, be that binary (like ProtoBuf) or same as system (like S-Expressions). Binary would even have the advantage of using the least space - especially of concern when sending such data over networks (else you're doing something similar to sending a DWG file as a DXF file, just so it can be converted back at the other end to DWG again).

in the simplest cases just like yours there is no need to parse json
Code: [Select]
(setq str "{\"attributes\": {\"color\": \"ffffaa00\",\"layer\": \"LAY012\",\"areaval\": 1351.014,},\"geometry\": {\"rings\": [[[5651.534,8026.656][5653.917,8083.191]]]}}")
(read (vl-string-translate "[]{}:," "()()  " str))
Very good idea ... though it's not exactly the same thing. E.g. the attributes object will become just a list of values, with attrib name followed by value. More correctly this should become an association list instead. And that sort of "where do quotes go" story is exactly the sort of problems to run into when re-implementing yet another JSON parser (it's actually a lot worse with XML as you need to also then parse open / close tags, meaning even spelling errors become an issue).
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: How to read JSON file format via lisp?
« Reply #4 on: October 03, 2016, 09:29:57 AM »
This sort of thing is why I'd discourage such formats if at all possible. They'd only be useful if you need the data in some other program which can only read/write JSon / XML.
sometimes json is a must when communicating with some internet services
for example i had to write some sort of json parser to get data from translate.google.com

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: How to read JSON file format via lisp?
« Reply #5 on: October 03, 2016, 11:11:09 AM »
sometimes json is a must when communicating with some internet services
for example i had to write some sort of json parser to get data from translate.google.com
That's why I've mentioned:
They'd only be useful if you need the data in some other program which can only read/write JSon / XML.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

petar_dimov

  • Mosquito
  • Posts: 10
Re: How to read JSON file format via lisp?
« Reply #6 on: October 04, 2016, 03:44:56 PM »
Thanks for your opinion. Аctually this is server responce and i must deal with it programmatically. Maybe ActiveX is best solution. Unfortunately VB6 don't support x64 and i have to write some wrapper or something similar on VB.NET. But can't uderstand why acad lisp wont connect to dll builded at "Any CPU" and have to buid separated x86 and x64 dll or i miss something?

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: How to read JSON file format via lisp?
« Reply #7 on: October 05, 2016, 07:08:16 AM »
But can't uderstand why acad lisp wont connect to dll builded at "Any CPU" and have to buid separated x86 and x64 dll or i miss something?
The issue is that AnyCPU doesn't mean what you think it means - it actually means it gets loaded as either 32bit or 64bit depending on which process loaded it.


You'll have to load the DLL in ACad itself. And that's most probably 64bit (at least these days), but things like VisualStudio (e.g. while you're debugging) tends to default to 32bit instead. So it's usually a better idea to make these specific to the targeted program.


The next issue you're going to run into is converting types between DotNet internals and AutoLisp dynamic types. You may want to look at some stuff done here: http://forums.augi.com/forumdisplay.php?1417-Bridging-the-Gap-LISP-NET-LISP
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.