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"}
> @gwl|amit
{projectDetails: [ {detail : {} }], name: "amit"}
> @$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
> @$
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)