none
Need some example for using new System.text.json RRS feed

  • Question

  • Hello:

    Today, I upgrade my IDE from Visual Studio 2019 Version 16.2.5 to 16.3.0 on Windows 10 (Version 1903).

    I want to try some new stuff, like System.text.json, if succeed, I can get rid of Newtonsoft.json.

    I used the following small function quite often, which uses Newtonsoft.json to check if a string is a json object:

    public static bool IsJsonObject(string stringValue)
    {
        if (string.IsNullOrWhiteSpace(stringValue))
        return false;
        string value = stringValue.Trim();
        if (value.StartsWith("{") && value.EndsWith("}"))
        {
        try
        {
        JToken obj = JToken.Parse(value);
        return true;
        }
        catch (JsonReaderException)
        {
        return false;
        }
        }
        return false;
    }
    

    However, I can't find good example on how to convert this function to use the new System.text.json.

    Please advice.

    Thanks,

    Monday, September 23, 2019 10:26 PM

Answers

  • Hi zydjohn,

    Thanks for the feedback.

    >>But the following code is OK: I have to use System.Text.Json.JsonException.

    I want to if you have any other question about it. If so, please feel free to let me know. If not, you could mark the useful reply as an answer.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by zydjohn Tuesday, September 24, 2019 9:56 PM
    Tuesday, September 24, 2019 5:51 AM
    Moderator

All replies

  • Hi zydjohn,

    Thank you for posting here.

    You could try the following code to detect if a string is a json object by using system.text.json.

    static void Main(string[] args)
            {
                string m = File.ReadAllText("D:\\test.json");
                bool t = IsJsonObject(m);
    
    
            }
    
            public static bool IsJsonObject(string stringValue)
            {
                if (string.IsNullOrWhiteSpace(stringValue))
                    return false;
                if (stringValue.StartsWith("{") && stringValue.EndsWith("}"))
                {
                    try
                    {
                        JsonDocument document = JsonDocument.Parse(stringValue);
                        return true;
                    }
                    catch (JsonException)
                    {
                        return false;
                    }
                }
                return false;
            }

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 24, 2019 2:40 AM
    Moderator
  • Hello:

    Thanks for your code, I did a test, but I got compiler error message:

    Error CS0104 'JsonException' is an ambiguous reference between 'Newtonsoft.Json.JsonException' and 'System.Text.Json.JsonException'.  Even I deleted Newtonsoft.Json nuget package and install only System.Text.Json package.

    But the following code is OK:  I have to use System.Text.Json.JsonException.

    public static bool IsJsonObject(string stringValue)
    {
        if (string.IsNullOrWhiteSpace(stringValue))
        return false;
        if (stringValue.StartsWith("{") && stringValue.EndsWith("}"))
        {
        try
        {
        JsonDocument document = JsonDocument.Parse(stringValue);
        return true;
        }
        catch (System.Text.Json.JsonException)
        {
        return false;
        }
        }
        return false;
    }
    

    Tuesday, September 24, 2019 3:48 AM
  • Technically, your code is not quite correct.  It might be that YOUR application requires that the overall JSON document be an object (meaning it starts with a { brace } ), but it's quite common for a JSON document to be an array, so it can start with a [ square bracket ].  And in fact, the spec says that a JSON document can be any JSON element.  So, the following 5 lines are all valid JSON documents:

        { "one" : "two" }

        [ 1, 2, 3, 4 ]

        "string"

        333

        false


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Tuesday, September 24, 2019 4:01 AM
  • Hi zydjohn,

    Thanks for the feedback.

    >>But the following code is OK: I have to use System.Text.Json.JsonException.

    I want to if you have any other question about it. If so, please feel free to let me know. If not, you could mark the useful reply as an answer.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by zydjohn Tuesday, September 24, 2019 9:56 PM
    Tuesday, September 24, 2019 5:51 AM
    Moderator