0
$\begingroup$

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)

$\endgroup$
  • $\begingroup$ Do you have a question? I don't see a question here. Coding questions are off-topic here. It sounds like you're trying to write a parser for some complicated format that I don't totally understand. So have you tried using standard parsing tools? Is there a reason why they are inadequate? If so, can you distill down to just the key challenge, so we don't have to understand the entire language you've got? $\endgroup$ – D.W. Feb 24 '18 at 6:50
  • $\begingroup$ 1. I believe it is difficult to ask for the solution without explaining the problem completely and referencing the sample code. However I've shifted the problem statement above. 2. No i'ts not about code, it's about algorithm logic. 3. Can you please suggest me some parser which can be customized for any problem? 4. I would be happy if you would like to edit the question to make it more explainable. $\endgroup$ – Amit Kumar Gupta Feb 24 '18 at 7:57
0
$\begingroup$

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
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.