Пример #1
0
        static void testGetStringValue()
        {
            JSONParse parser = new JSONParse("\"HelloWorld\"");

            System.assertEquals("HelloWorld", parser.getStringValue());
            parser = new JSONParse("true");
            System.assertEquals("true", parser.getStringValue());
            parser = new JSONParse("null");
            System.assertEquals(null, parser.getStringValue());
            parser = new JSONParse("12.5");
            System.assertEquals("12.5", parser.getStringValue());
            try
            {
                parser = new JSONParse("{}");
                parser.getStringValue();
                System.assert(false, "Node is not a valid String, should have seen an exception about that.");
            }
            catch (JSONParse.InvalidConversionException e)
            {
                System.assertEquals("Objects and arrays are not Strings: { }", e.getMessage());
            }

            try
            {
                parser = new JSONParse("[]");
                parser.getStringValue();
                System.assert(false, "Node is not a valid String, should have seen an exception about that.");
            }
            catch (JSONParse.InvalidConversionException e)
            {
                System.assertEquals("Objects and arrays are not Strings: [ ]", e.getMessage());
            }
        }
Пример #2
0
        static void testRawString()
        {
            JSONParse parser = new JSONParse("\"HelloWorld\"");

            System.assertEquals(false, parser.isObject());
            System.assertEquals(false, parser.isArray());
            System.assertEquals("HelloWorld", parser.getStringValue());
        }
Пример #3
0
        static void testGetBlobValue()
        {
            Blob      helloWorld = Blob.valueOf("HelloWorld");
            string    encoded    = EncodingUtil.base64Encode(helloWorld);
            JSONParse parser     = new JSONParse("\"" + encoded + "\"");

            System.assertEquals(encoded, parser.getStringValue());
            System.assertEquals(helloWorld, parser.getBlobValue());
            try
            {
                parser = new JSONParse("42");
                parser.getBlobValue();
                System.assert(false, "Node is not a valid Blob, should have seen an exception about that.");
            }
            catch (JSONParse.InvalidConversionException e)
            {
                System.assertEquals("Only String values can be converted to a Blob: 42", e.getMessage());
            }
        }