JSON format in the dxf xRecord

JSON format in the dxf xRecord

antti.kujala
Contributor Contributor
2,614 Views
7 Replies
Message 1 of 8

JSON format in the dxf xRecord

antti.kujala
Contributor
Contributor

Hi,

 

We are storing data as JSON format in the DBObjects extension dictionaries. Everything is working well except when the dwg is saved as dxf. The saved dxf file cannot be opened by the autocad as the xrecord entries containing the JSON cannot be read. The AcDbXrecord in dxf looks something like this.

 

 

AcDbXrecord
280
     1
  1
{"Setups":[{"Name":"Default","Default":[1,2,4,8,16],"ID":"00000000-0000-0000-0000-000000000000","Steps":[]}],"SelectedID":"00000000-0000-0000-0000-000000000000"}
  0
XRECORD
  5
25B
102
{ACAD_REACTORS
330
25A
102
}
330
25A
100

 

 

The problem seems to be the '{' and '}' characters int he JSON format. Is the only way to replace the characters in the JSON string with something else ? Or is there a better solution ?

0 Likes
Accepted solutions (1)
2,615 Views
7 Replies
Replies (7)
Message 2 of 8

dgorsman
Consultant
Consultant

Xrecord strings should be able to handle every character.  It could be the string formatting - are you handling all those interior double-quotes properly so it sees it as one string?

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes
Message 3 of 8

ActivistInvestor
Mentor
Mentor

Are you getting any error during DXFIN, or when opening the DXF file ?

 

If there was problem reading the strings, I would expect it to manifest in the LISP (entmake) function as well, but it doesn't seem to have any problem with your json string:

 

 

Command: (setq strdata (getstring T))
{"Setups":[{"Name":"Default","Default":[1,2,4,8,16],"ID":"00000000-0000-0000-0000-000000000000","Steps":[]}],"SelectedID":"00000000-0000-0000-0000-000000000000"}
"{\"Setups\":[{\"Name\":\"Default\",\"Default\":[1,2,4,8,16],\"ID\":\"00000000-0000-0000-0000-000000000000\",\"Steps\":[]}],\"SelectedID\":\"00000000-0000-0000-0000-000000000000\"}"

Command: (princ strdata)
{"Setups":[{"Name":"Default","Default":[1,2,4,8,16],"ID":"00000000-0000-0000-0000-000000000000","Steps":[]}],"SelectedID":"00000000-0000-0000-0000-000000000000"}"

Command: (setq xrecord (entmakex (list (cons 0 "XRECORD") (cons 100 "AcDbXrecord") (cons 1 strdata))))
<Entity name: 7ffffb0c6e0>

Command: (entget xrecord)
((-1 . <Entity name: 7ffffb0c6e0>) (0 . "XRECORD") (330 . <Entity name: 0>) (5 . "286") (100 . "AcDbXrecord") (280 . 1) (1 . "{\"Setups\":[{\"Name\":\"Default\",\"Default\":[1,2,4,8,16],\"ID\":\"00000000-0000-0000-0000-000000000000\",\"Steps\":[]}],\"SelectedID\":\"00000000-0000-0000-0000-000000000000\"}"))

Command: (setq str (cdr (assoc 1 (entget xrecord))))
"{\"Setups\":[{\"Name\":\"Default\",\"Default\":[1,2,4,8,16],\"ID\":\"00000000-0000-0000-0000-000000000000\",\"Steps\":[]}],\"SelectedID\":\"00000000-0000-0000-0000-000000000000\"}"

Command: (eq str strdata)
T

 

 

 


@antti.kujala wrote:

Hi,

 

We are storing data as JSON format in the DBObjects extension dictionaries. Everything is working well except when the dwg is saved as dxf. The saved dxf file cannot be opened by the autocad as the xrecord entries containing the JSON cannot be read. The AcDbXrecord in dxf looks something like this.

 

 

AcDbXrecord
280
     1
  1
{"Setups":[{"Name":"Default","Default":[1,2,4,8,16],"ID":"00000000-0000-0000-0000-000000000000","Steps":[]}],"SelectedID":"00000000-0000-0000-0000-000000000000"}
  0
XRECORD
  5
25B
102
{ACAD_REACTORS
330
25A
102
}
330
25A
100

 

 

The problem seems to be the '{' and '}' characters int he JSON format. Is the only way to replace the characters in the JSON string with something else ? Or is there a better solution ?


 

0 Likes
Message 4 of 8

antti.kujala
Contributor
Contributor

Hi, 

 

Thanks for the reply 

 

The error comes with both DXFIN or when opening the file with browser. 

 

The problem is clearly related to the { and } characters. Replacing those for example with their unicodes U+007B and U+007D fixes the problem. Unfortunately with this method the unicodes needs to be replaced to curly brackets before deserializing the string back to an object. 

 

 

U+007B"Setups":[U+007B"Name":"Default","Default":[1,2,4,8,16],"ID":"00000000-0000-0000-0000-000000000000","Steps":[]U+007D],"SelectedID":"00000000-0000-0000-0000-000000000000"U+007D

Here is the dxf file attached, If you can figure out something from the file. I am using ACAD 2016

 

0 Likes
Message 5 of 8

ActivistInvestor
Mentor
Mentor

It looks like the problem may not the curly-braces. The problem may be the length of the string. While it may not be a problem with DWG, it could be a problem with DXF.  To confirm that, write a DXF that contains some short json strings that are < 1024 characters long. If it still fails, then try again with shorter strings. I suspect the problem is the length of the strings, but in any event, that can't be solved. You can also convert the entire string to Base64 and then convert back after the DXFIN or when you read the Xrecord.

 

 

 


@antti.kujala wrote:

Hi, 

 

Thanks for the reply 

 

The error comes with both DXFIN or when opening the file with browser. 

 

The problem is clearly related to the { and } characters. Replacing those for example with their unicodes U+007B and U+007D fixes the problem. Unfortunately with this method the unicodes needs to be replaced to curly brackets before deserializing the string back to an object. 

 

 

U+007B"Setups":[U+007B"Name":"Default","Default":[1,2,4,8,16],"ID":"00000000-0000-0000-0000-000000000000","Steps":[]U+007D],"SelectedID":"00000000-0000-0000-0000-000000000000"U+007D

Here is the dxf file attached, If you can figure out something from the file. I am using ACAD 2016

 


 

0 Likes
Message 6 of 8

_gile
Consultant
Consultant
Accepted solution

Hi,

 

I think the problem is due to the string length.

 

The limit is  2049 characters (see >>here<<) and the xrecord which causes the error (starting line 3076) has 4775 characters (note there's another xrecord -starting line 3058- which contains curly brackets and did not throw an error).

If you remove this xrecord, the dxf file can be loaded in AutoCAD without error.

 

You should split this string into several xrecords.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 8

ActivistInvestor
Mentor
Mentor

@_gile's reply confirms that there's a max length on strings.

 

You don't have to split the string into multiple Xrecords, just multiple fields within the same Xrecord. A single Xrecord has a theoretical size limit of 2GB.

 

You can write an integer field before you write multiple string fields, indicating how many string fields follow, if that makes things simpler. You can also bracket multiple string fields as a way of identifying them as being the contents of a single string split into multiple fields.

 

If you're writing DXF files, then it's presumed that they're going to be read by non-AutoCAD or non-Autodesk products, the problem there is that unless you know what products are reading the files, and what specific limits they may have on DXF string data, the best course of action is to avoid long strings and keep their length below the limit imposed by older DXF formats.

0 Likes
Message 8 of 8

antti.kujala
Contributor
Contributor

Hi, 

 

The problem was the length of the result buffers. I had it set to  32768 which was the half of 65536 limit with the dwg format. 

Setting the string chunk size to 512 fixed the problem with the dxf

 

In addition if  { and } where replaced with Unicode acad seems just ignore the records. When the record is starting with { error is produced. 

 

Thanks! 

 

 

 

0 Likes