0

I need to define a layout configuration file as follows

{
  'row' : {
    'height' : 20%,
    'content' : 'BLOCK_MENU'
  },
  'row' : {
    'col': {
      'width' : 30%,
      'content': 'BLOCK_SIDEBAR'
    },
    'col' : {
      'width' : 70%,
      'content' : 'BLOCK_MAIN_CONENT'
    }
  }
}

But this is neither a valid javascript object nor a json. I need to convert it to json or js object. Since this configuration file is to be written by user I cannot make the format too strict and complicated. Is there any way to do this without using LEX or bison? I'm using nodejs at server side.

7
  • 1
    Well, you would only need quotes around the width to make it work as JSON or JS object. That doesn't seem to be much of a complication. Commented Mar 26, 2014 at 15:37
  • 1
    Why do you need the percentage character in the data? You should just keep that field as an integer and add the symbol on the client. Commented Mar 26, 2014 at 15:38
  • 1
    @aioros: JSON requires double quotes for strings. But it would be a valid JS object literal at least. Commented Mar 26, 2014 at 15:42
  • use double quotes.. it's the JSON standard :/ Commented Mar 26, 2014 at 15:42
  • @Aioros Actually my problem is duplicate keys.. there are 2 'row' element in the above text, also 2 'col' inside one 'row' element. Commented Mar 26, 2014 at 15:59

3 Answers 3

2

If you need duplicate keys, probably you don't need keys at all. Your object can be an array of row objects, and each row can have an array of column objects. Something like this:

[
    {
        "height" : "20%",
        "content" : "BLOCK_MENU"
    },
    {
        "height": "something",
        "content": "whatever",
        "cols": [
            {
                "width" : "30%",
                "content": "BLOCK_SIDEBAR"
            },
            {
                "width" : "70%",
                "content" : "BLOCK_MAIN_CONENT"
            }
        ]
    }
]
Sign up to request clarification or add additional context in comments.

2 Comments

is this a typo? I think your missing a "coma" after "whatever"
Thank you.. I think I can adjust with this format, this will be far better than writing custom parser.
1

How about changing the file format to this:

[
  {
    "height" : "20%",
    "content" : "BLOCK_MENU"
  },
  {
    "cols": [
      {
        "width" : "30%",
        "content": "BLOCK_SIDEBAR"
      },
      {
        "width" : "70%",
        "content" : "BLOCK_MAIN_CONENT"
      }
    ]
  }
]

The outer array is an array of rows. A row has an optional "cols" array, which is an array of columns.

If height and width are always going to be a percentage, I might even replace the percentage values so that your code won't have to convert a string to a number:

[
  {
    "height" : 20,
    "content" : "BLOCK_MENU"
  },
  {
    "cols": [
      {
        "width" : 30,
        "content": "BLOCK_SIDEBAR"
      },
      {
        "width" : 70,
        "content" : "BLOCK_MAIN_CONENT"
      }
    ]
  }
]

2 Comments

.2 wont validate, but "2" will
@Plude: Thanks. Turns out that JSON requires numbers to start with an integer. So, I've modified my answer to simply use whole numbers to represent the percentage. 20 seems more intuitive than 0.2 to represent 20%.
0
{
    "row": {
        "col": { "width": "70%", "content": "BLOCK_MAIN_CONENT" }
    }
}

this would validate as json

validate here Validate Json

or

{
    "row" : {
        "col_1": { "width" : "30%", "content": "BLOCK_SIDEBAR" },
        "col_2" : { "width" : "70%", "content" : "BLOCK_MAIN_CONENT" }
    }
}

3 Comments

I need one more 'col' to be defined in the 'row' element.
can you have col_1 and col_2? this would verify as well "col_1": { "width" : "30%", "content": "BLOCK_SIDEBAR" }, "col_2" : { "width" : "70%", "content" : "BLOCK_MAIN_CONENT" }
This is the temporary solution that I have chosen now. I think I have to write my own parser to parse that using jison

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.