javascript - Map JSON-pointer to the {line, column} in a text JSON file - Stack Overflow

I need to map a location in JSON (or JavaScript object) defined by JSON-pointer to the position in JSON

I need to map a location in JSON (or JavaScript object) defined by JSON-pointer to the position in JSON text file as {line,column}. Is there any existing JavaScript library that would do it? Writing this code is going to be a bit tedious...

For example, if I have a JSON file (text):

{
  "foo": [
    {
      "bar": 1
    }
  ]
}

then given JSON-pointer /foo/0/bar I need to get {line: 4, column: 7} as the result.

If an equivalent JSON value is stored in this JSON file:

{"foo":[{"bar":1}]}

then the result for the same JSON pointer should be {line: 1, column: 10}.

I need to map a location in JSON (or JavaScript object) defined by JSON-pointer to the position in JSON text file as {line,column}. Is there any existing JavaScript library that would do it? Writing this code is going to be a bit tedious...

For example, if I have a JSON file (text):

{
  "foo": [
    {
      "bar": 1
    }
  ]
}

then given JSON-pointer /foo/0/bar I need to get {line: 4, column: 7} as the result.

If an equivalent JSON value is stored in this JSON file:

{"foo":[{"bar":1}]}

then the result for the same JSON pointer should be {line: 1, column: 10}.

Share asked Jan 8, 2017 at 18:04 espesp 7,7276 gold badges57 silver badges85 bronze badges 4
  • If it's okay to only handle specifically formatted JSON, you can push the problem back to doing a clone of the JSON, generate some guid, put the guid in that path and stringify, use string searching for the guid – Paul S. Commented Jan 8, 2017 at 18:08
  • It won't work, as path can point to a scalar value (like in example) and there is no way to add some UUID to this value. You can add it to a parent object, but I need to map all paths. You can of course replace scalar values with some arrays/objects, but then it would change positions in the original file below (even if it's specifically formatted)... Also, ideally I need mappings to original source files, rather than in specifically formatted JSON. – esp Commented Jan 8, 2017 at 18:09
  • 1 Seems like you will need to write a customer parser/interpreter. Either see if you can build on an existing JSON implementation or start writing your own jsbin./xixuyiwiwi/edit?js,output – Paul S. Commented Jan 8, 2017 at 18:51
  • I've done the opposite task so far: generate mappings while stringifying. github./epoberezkin/json-source-map – esp Commented Jan 9, 2017 at 0:09
Add a ment  | 

1 Answer 1

Reset to default 6

This library has the equivalents of JSON.parse/stringify that also return mappings: https://github./epoberezkin/json-source-map

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744931778a4601784.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信