Giter Site home page Giter Site logo

Comments (5)

NETertainer avatar NETertainer commented on June 10, 2024 1

Sorry for bothering. I misinterpreted the output of the browser console. You are totaly right and I got it wrong.

from mongoose.

scaprile avatar scaprile commented on June 10, 2024

Would you be so kind to fill the issue template ? I'm sure this will be a great reading once you decode what you want to do and what you got, for me, so I don't have to infer that from your code and your outputs. If this is our bug, we'll fix it sooner.
Yeah, the title helps, but please, will you ?
Thank you.

from mongoose.

NETertainer avatar NETertainer commented on June 10, 2024

Sorry for being lazy 😄. Updated.

from mongoose.

scaprile avatar scaprile commented on June 10, 2024

The first test string seems to be the escape of an already escaped \n , that is \\n.
"{\"1\": \"a\\\\nb\"}" is C for {"1": "a\\nb"}. The double backslash is unescaped and the string returned as {"1": "a\nb"}.

The second test string to be parsed contains a backslash and its corresponding character.
"{\"1\": \"a\\nb\"}" is C for {"1": "a\nb"}, that is what mg_json_unescape() sees and correctly processes, so the output would be {"1": "ab"}

mongoose/src/json.c

Lines 292 to 293 in 6386cdf

// \uXXXX escape. We could process a simple one-byte chars
// \u00xx from the ASCII range. More complex chars would require

See real stuff with `mg_hexdump()"

   char *test = "{\"1\": \"a\\\\nb\"}";
   char* temp = mg_json_get_str(mg_str(test), "$.1");
   MG_INFO(("In:"));
   mg_hexdump(test, strlen(test));
   MG_INFO(("Out:"));
   mg_hexdump(temp, strlen(temp));
   

   test = "{\"1\": \"a\\nb\"}";
   temp = mg_json_get_str(mg_str(test), "$.1");
   MG_INFO(("In:"));
   mg_hexdump(test, strlen(test));
   MG_INFO(("Out:"));
   mg_hexdump(temp, strlen(temp));
$ ./fr13 
4d1fb5a 2 fr13.c:7:main                 In:
0000   7b 22 31 22 3a 20 22 61 5c 5c 6e 62 22 7d         {"1": "a\\nb"}  
4d1fb5a 2 fr13.c:9:main                 Out:
0000   61 5c 6e 62                                       a\nb            
4d1fb5a 2 fr13.c:15:main                In:
0000   7b 22 31 22 3a 20 22 61 5c 6e 62 22 7d            {"1": "a\nb"}   
4d1fb5a 2 fr13.c:17:main                Out:
0000   61 0a 62                                          a.b             

What I think you are observing is your JavaScript console processing the first \n and escaping it for you when printing it.

I'd like to see your actual use case and whether this has an actual problem that needs to be solved.

from mongoose.

NETertainer avatar NETertainer commented on June 10, 2024

What I was trying to point out is that "a\nb" is not valid json and the \n should not lead to being parsed as new line, while "a\\nb" is valid json (double escaping is strictly required) but is not unescaped at all.

JSON.parse('"a\nb"')
Uncaught SyntaxError: 

JSON.parse('"a\\nb"')
'a\nb'

edit:
JSON.stringify(`a
b`)
'"a\\nb"'

I only used console.log to visualize the new line when the valid json gets unescaped in e.g. a browser.

So the current implementation just does not work with valid json and additionally accepts invalid json that can not be used otherwise.

from mongoose.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.