JSON serializer - slight bug
Wednesday, 30 September 2015 · 3 messages
HR
Hans-Juergen Rennau30 Sep 2015 14:22 #
Dear BaseX,
I think there is a very slight bug in the JSON serializer: when an element with type="object" has a <_> child, I think the serializer should report an error (as underscores must be doubled, if not using the name "_", reserved for array items. Instead, the serializer creates a property with the empty string as name. Example:
json:serialize(
<json type="object">
<BaseX type="object">
<_>is phantastic</_>
</BaseX>
</json>
)=>{
"BaseX":{
"":"is phantastic"
}
}Compare with:
json:serialize(
<json type="object">
<BaseX type="object">
<__>is phantastic</__>
</BaseX>
</json>
)=>{
"BaseX":{
"_":"is phantastic"
}
}It may look like pedantry, but this misbehaviour may cause confusion, as it is of course an interesting practise to create JSON by creating the XML format and in the end serializing: if then inspecting the end result, it is not immediately clear that the "":... is caused by the serializer, rather than by own code.
Kind regards,Hans-Jürgen
CG
Christian Grün30 Sep 2015 14:27 #
Hi Hans-Jürgen,
You may be surprised, but the behavior is correct. It's defined in the conversion rules in our documentation [1]: "
Empty names are represented by a single underscore (_).". The following example illustrates the behavior:
json:parse('{ "":"" }')Hope this helps,
Christian
[1] http://docs.basex.org/wiki/Json_Module#Direct
Quoted text · Hans-Juergen Rennau · 47 lines
On Wed, Sep 30, 2015 at 2:22 PM, Hans-Juergen Rennau <hrennau at yahoo.de> wrote: > Dear BaseX, > > I think there is a very slight bug in the JSON serializer: when an element > with type="object" has a <_> child, I think the serializer should report an > error (as underscores must be doubled, if not using the name "_", reserved > for array items. Instead, the serializer creates a property with the empty > string as name. Example: > > json:serialize( > <json type="object"> > <BaseX type="object"> > <_>is phantastic</_> > </BaseX> > </json> > ) > => > { > "BaseX":{ > "":"is phantastic" > } > } > > Compare with: > > json:serialize( > <json type="object"> > <BaseX type="object"> > <__>is phantastic</__> > </BaseX> > </json> > ) > => > { > "BaseX":{ > "_":"is phantastic" > } > } > > It may look like pedantry, but this misbehaviour may cause confusion, as it > is of course an interesting practise to create JSON by creating the XML > format and in the end serializing: if then inspecting the end result, it is > not immediately clear that the "":... is caused by the serializer, rather > than by own code. > > Kind regards, > Hans-Jürgen
HR
Hans-Juergen Rennau30 Sep 2015 14:33 #
Oh thank you - now I understand.Of course, you needed a rule to represent zero-length string names, and "_" was the almost obvious choice! Sorry for the disturbance -Hans-Jürgen
Quoted text · Christian Grün · 1 line
Christian Grün <christian.gruen at gmail.com> schrieb am 14:27 Mittwoch, 30.September 2015:
Hi Hans-Jürgen,
You may be surprised, but the behavior is correct. It's defined in the conversion rules in our documentation [1]: "
Empty names are represented by a single underscore (_).". The following example illustrates the behavior:
json:parse('{ "":"" }')Hope this helps,
Christian
[1] http://docs.basex.org/wiki/Json_Module#Direct
Quoted text · Hans-Juergen Rennau · 47 lines
On Wed, Sep 30, 2015 at 2:22 PM, Hans-Juergen Rennau <hrennau at yahoo.de> wrote: > Dear BaseX, > > I think there is a very slight bug in the JSON serializer: when an element > with type="object" has a <_> child, I think the serializer should report an > error (as underscores must be doubled, if not using the name "_", reserved > for array items. Instead, the serializer creates a property with the empty > string as name. Example: > > json:serialize( > <json type="object"> > <BaseX type="object"> > <_>is phantastic</_> > </BaseX> > </json> > ) > => > { > "BaseX":{ > "":"is phantastic" > } > } > > Compare with: > > json:serialize( > <json type="object"> > <BaseX type="object"> > <__>is phantastic</__> > </BaseX> > </json> > ) > => > { > "BaseX":{ > "_":"is phantastic" > } > } > > It may look like pedantry, but this misbehaviour may cause confusion, as it > is of course an interesting practise to create JSON by creating the XML > format and in the end serializing: if then inspecting the end result, it is > not immediately clear that the "":... is caused by the serializer, rather > than by own code. > > Kind regards, > Hans-Jürgen
Written for BaseX 8.3. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery