How to write decoder for compressed stringified objects Aim: schema aware transformation.

I've written a code(in js) which can transform data {projectDetails: [ {detail : {}, id : "gwl" }], name: "amit"} into string @gwl|amit. But I'm not able to complete the decoder to transform it back.

Problem: logic to transform string back to object

In detail

Consider that following schema is given to transform data into string and back to object.

var schema = {
    type: "object",
    properties: {
        "projectDetails" : {
            type: "array",
            properties: {
                "detail" : {
                    type: "object",
                    properties: {
                        "first" : { type: "string"},
                        "last" : { type: "string"}
                    }
                },
                "id" : { type: "string"}
            }
        },
        "name" : { type: "string"}
    }
}


Consider following chars to understand the transformation;


| : boundary indicator. To separate consecutive dynamic data fields.
^ : next field(set of fields) is/are repeated array item
$ : empty value
# : missing, null, undefined field
@ : empty object or array
! : missing, null, undefined object or array
Y : True
N : False


Example: Transform

{projectDetails: [ {detail : {}, id : "gwl" }], name: "amit"}
&gt; @gwl|amit

{projectDetails: [ {detail : {} }], name: "amit"}
&gt; @$amit


Problem: transform string back to object

Implementation

I iterated the schema to updated every data field with possible next char may occur in transformed string to identify the boundary of current field. It can help to reduce number of comparisons while decoding. 

E.g. for the field id in above schema next possible chars in transformed string can be : [$^|#]. 

Since this way complicate the solution, following regular expression can be used (.*?)([|^#@!YN]) to read next field always. (it returns 2 matches every time, if first match is empty just ignore it and read further.)

Challenge

&gt; @$


For above string, my decoder fails. Why? Because this is how I transform the string back.


I read the schema.
Since projectDetails is an array type, I loop
Since array's item is of type object, I use recursion
detail is object type and since the first field value in string is @. I mark it empty
I read next field and mark it with $
I come out from the recursion. 
Since there is no array separator, I read next field of the root object.
I find nothing in transformed string to read.


Note I don't want to convert transformed string into array of fields. It'll cause issue in case of big objects.(I've faced it with other project)
Found the answer.

As described in question about encoding schema, there can be multiple objects falling to same encoded string. Hence I was facing the difficulty to write the decoder.

To solve the above problem, I've added 2 more chars to know the start of the object/array;

| : boundary indicator. To separate consecutive dynamic data fields.
^ : next field(set of fields) is/are repeated array item
$ : empty value
# : missing, null, undefined field
@ : empty object or array
! : missing, null, undefined object or array
Y : True
N : False
{ : object start
[ : array start