<p>I understand that deserializing data from a string or binary stream into a data structure is effectively the same parsing. When you deserialize the input string, you use a grammar to create a parse tree from that input, and that parse tree is effectively the encoded data structure.</p>
<p>For example, you may have an array of arrays encoded into a JSON string. When you deserialize that input string, you use the JSON grammar to parse the input string. The parse tree you derive from that input has the same structure as the data that was encoded into the string (in this case an array of arrays).</p>
<p>It seems like most data serialization formats (JSON, YAML, Protobuf) use context free grammars. These grammars are capable of encoding data structures like key value pairs, arrays, and trees.</p>
<p>My question:
Are there any data structures that cannot serialized using a context free grammar and require a more powerful grammar such as a context sensitive grammar? Or is it possible to serialize any data structure using a context free grammar, but you may need a longer string than if you were to user a more powerful grammar?</p>

CC BY-SA 4.0
2023-04-14T10:58:23.897Z
I think that even the serialization/deserialization of a simple matrix requires a more powerful grammar; how do you check with a CF grammar that all rows (or columns) have the same size?
Marzio De Biasi
CC BY-SA 4.0
2023-04-18T19:06:04.207Z
More generally, the approach falls short once your data structure has integrity constraints. You can use JSON schema then. But this discussion thread about features lacking in JSON schema refers to things you cannot put into a context-free description (partial ordering, unique keys): https://github.com/json-schema-org/json-schema-vocabularies/issues/22
Hermann Gruber
CC BY-SA 4.0
2023-04-18T19:14:48.293Z
...and for that reason, today we deserialize data into data transfer objects (DTOs), which don't fulfill any integrity constraints. Typically, all fields are nullable and so on. These DTOs are only then mapped to Business Objects, which have integrity constraints aka business invariants. The integrity constraints are thus validated only in the mapping step, not already in the deserialization. We may say that the shortcomings of JSON govern what the deserialized objects look like (they are dumb DTOs, not smart Business Objects).
Hermann Gruber
CC BY-SA 4.0
2023-07-11T14:43:03.247Z
Ah! I think I understand now (sorry for the slow reply).

If I am understanding correctly, an integrity constraint in this case may be something like, an array of tuples that are all of equal width, or something like that. That is inherently not context free.


But you could parse the serialized data, and extract all of the tuples while ignoring their length, then verify after parsing that all the tuples are the same length.

So, in the language of the comment above, the data structure with the variable length tuples would be the DTO without any integrity constraints.

thanks all!
bcarlborg
<fl.formal-languages><ds.data-structures><context-free><grammars><parsing>
Are there data structures that cannot be serialized / deserialized using a context free grammar?
bcarlborg
# Are there data structures that cannot be serialized / deserialized using a context free grammar?

(Asked by: username_0 on 2023-04-14)

I understand that deserializing data from a string or binary stream into a data structure is effectively the same parsing. When you deserialize the input string, you use a grammar to create a parse tree from that input, and that parse tree is effectively the encoded data structure.

For example, you may have an array of arrays encoded into a JSON string. When you deserialize that input string, you use the JSON grammar to parse the input string. The parse tree you derive from that input has the same structure as the data that was encoded into the string (in this case an array of arrays).

It seems like most data serialization formats (JSON, YAML, Protobuf) use context free grammars. These grammars are capable of encoding data structures like key value pairs, arrays, and trees.

My question:
Are there any data structures that cannot serialized using a context free grammar and require a more powerful grammar such as a context sensitive grammar? Or is it possible to serialize any data structure using a context free grammar, but you may need a longer string than if you were to user a more powerful grammar?

**Tags:** fl.formal-languages, ds.data-structures, context-free, grammars, parsing

> **Comments:**
> **username_1**: I think that even the serialization/deserialization of a simple matrix requires a more powerful grammar; how do you check with a CF grammar that all rows (or columns) have the same size?  
> **username_2**: More generally, the approach falls short once your data structure has integrity constraints. You can use JSON schema then. But this discussion thread about features lacking in JSON schema refers to things you cannot put into a context-free description (partial ordering, unique keys): https://github.com/json-schema-org/json-schema-vocabularies/issues/22  
> **username_2**: ...and for that reason, today we deserialize data into data transfer objects (DTOs), which don't fulfill any integrity constraints. Typically, all fields are nullable and so on. These DTOs are only then mapped to Business Objects, which have integrity constraints aka business invariants. The integrity constraints are thus validated only in the mapping step, not already in the deserialization. We may say that the shortcomings of JSON govern what the deserialized objects look like (they are dumb DTOs, not smart Business Objects).  
> **username_0**: Ah! I think I understand now (sorry for the slow reply).  If I am understanding correctly, an integrity constraint in this case may be something like, an array of tuples that are all of equal width, or something like that. That is inherently not context free.   But you could parse the serialized data, and extract all of the tuples while ignoring their length, then verify after parsing that all the tuples are the same length.  So, in the language of the comment above, the data structure with the variable length tuples would be the DTO without any integrity constraints.  thanks all!  

---

## 0 answers

