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

"\0" is invalid JSON #52

Closed
czechdude opened this issue Jul 24, 2017 · 3 comments
Closed

"\0" is invalid JSON #52

czechdude opened this issue Jul 24, 2017 · 3 comments

Comments

@czechdude
Copy link

Hi,
we have the same problem as stated here for the Java version
alibaba/fastjson#118

so the rest is just copied:

According the JSON specification, "\0" is not a valid JSON string and must be encoded with four hexadecimal bytes. It's not just a theoretical problem - some python JSON parsers do not accept it.

I recommend the following unit test:

public class FastJsonTest {
    @Test
    public void testEncodeNulByte() throws Exception {
        String json = JSON.toJSONString("\0");
        assertEquals("\"\\u0000\"", json);
    }
}
@mgholam
Copy link
Owner

mgholam commented Jul 24, 2017

The following works as expected in the tests i.e. you get \u0000

    [Test]
    public static void zerostring()
    {
        var s = fastJSON.JSON.ToJSON("\0");
        Console.WriteLine(s);
        var o = fastJSON.JSON.ToObject(s);
        Console.WriteLine("" + o);
    }

@mgholam mgholam closed this as completed Jul 24, 2017
@czechdude
Copy link
Author

czechdude commented Jul 24, 2017

but wait that is wrong!

I wrote a test and it does not do what is expected:

	    [Test]
	    public static void SerializeStringWithZeroEscapeTest()
	    {

	        fastJSON.JSONParameters param = new JSONParameters();
	        param.SerializeNullValues = false;
	        param.UseExtensions = false;
	        param.UseFastGuid = false;
	        param.UseEscapedUnicode = true;
	        var testClass01 = new TestClass01();
	        testClass01.stringid = "Test\0Test";

	        var newJson = JSON.ToJSON(testClass01,param);
	        Assert.True(newJson.Contains("\\u0000"), "wrong repre of \\0");

	        param = new JSONParameters();
	        param.SerializeNullValues = false;
	        param.UseExtensions = false;
	        param.UseFastGuid = false;
	        param.UseEscapedUnicode = false;
	        testClass01 = new TestClass01();
	        testClass01.stringid = "L6645230045235\00175";

	        newJson = JSON.ToJSON(testClass01, param);
	        Assert.True(newJson.Contains("L6645230045235\\00175"), "wrong repre of without escaping unicode \\0");
        }

we are trying to transfer stringids with value "L6645230045235\00175"

@mgholam
Copy link
Owner

mgholam commented Jul 24, 2017

Fixed in v2.1.25

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

2 participants