Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line numbers in tokens #146

Open
kjhermans opened this issue Oct 30, 2018 · 7 comments
Open

Line numbers in tokens #146

kjhermans opened this issue Oct 30, 2018 · 7 comments

Comments

@kjhermans
Copy link

May I suggest we have line numbers in tokens? It would help greatly to debug faulty JSON files.

@pt300
Copy link
Collaborator

pt300 commented Oct 30, 2018

Tokens already store their position in the input string. I see no point in implementing what you suggested as this would needlessly increase use of memory by JSMN.

@kjhermans
Copy link
Author

If you individually popped tokens (for example, through a callback), or if there were a way to get a more proper error message (the main structure can store the current line) on error?

@pt300
Copy link
Collaborator

pt300 commented Oct 30, 2018

Seems like what you want is a fully featured JSON parsing library. If that's the case then I'd suggest you to look for alternatives. JSMN is quite minimalistic and has a bit of problems.

@kjhermans
Copy link
Author

No, not really. What I really, really appreciate about jsmn is the terseness of the library, which gives me a good indication of they way it will hold up in a security evaluation (# of LoC). My comment about the #ifdef in the typedef comes from this perspective (makes it less secure, because it may inadvertently be interpreted differently by the library compiler and the compiled-library user). My comment about the line numbering was more of me translating an end-user complaint (end-user likes to know where the parsing goes wrong), and I don't care that much about it myself. Besides - because I know the position in the JSON string, I can retroactively recover the line number anyway, by counting it myself.

@pt300
Copy link
Collaborator

pt300 commented Oct 31, 2018

I see what you mean. But for the line numbering I'd be more interested in specific point where the error happened, not line. That's because I could have JSON data which is not neatly formatted and has a lot happening in one line.

@kjhermans
Copy link
Author

kjhermans commented Nov 1, 2018

Something like this?

/**
 * Counts the number of lines in [string], until [pos] is reached.
 * Calculates the x,y position inside [string], of [pos].
 * Returns the x,y position of a position in a string.
 *
 * \param string  Zero-terminated string, preferably longer than [pos] bytes.
 * \param pos     Position inside the string.
 * \param vector  Contains the line (off-by-one) [0], and position on that
 *                line (off-by-one) [1], on successful return.
 *
 * \returns       Zero on success (pos is inside string), or non-zero on error.
 */
int strxypos
  (char* string, unsigned pos, unsigned vector[ 2 ])
{
  unsigned i = 0;

  vector[ 0 ] = 1;
  vector[ 1 ] = 1;
  while (i < pos) {
    switch (string[ i++ ]) {
    case '\n':
      ++(vector[ 0 ]);
      vector[ 1 ] = 0;
      break;
    case 0:
      return ~0;
    }
    ++(vector[ 1 ]);
  }
  return 0;
}

@dominickpastore
Copy link

Line numbers could be useful, but many users of jsmn are on memory constrained systems. If such features are added, they should probably be wrapped in #ifdef JSMN_LINENO (or similar), like we have for parent links.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants