Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a primitive type considered JSON?

Tags:

json

Most place the JSON is in format like

{
    color: "red",
    value: "#f00"
}

Or

[  
    { color: "red",     value: "#f00"   },
    { color: "red",     value: "#f00"   }
]

And I want to ask is primitive type like string, bool, int also JSON?

I have found follow link,

http://json-schema.org/latest/json-schema-core.html

http://www.json.org/

https://zh.wikipedia.org/wiki/JSON

https://www.ietf.org/rfc/rfc4627.txt

http://www.c-sharpcorner.com/uploadfile/3d39b4/data-types-in-json/

And,

In RFC4627 it says

JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays).

A string is a sequence of zero or more Unicode characters [UNICODE].

An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.

An array is an ordered sequence of zero or more values.

The terms "object" and "array" come from the conventions of JavaScript.

So I reading it as pure string, number boolean like

"a string"

100

true

Are all JSON,

But two of my colleagues think that primitive types can only be value in object,

{ ASTRING : "astring" } is JSON,

And if there's only "a string", this is not called, as it is not JSON format,

I think I, and my colleagues may not be professional enough to judge it,

So I want to know, is pure primitive type JSON?

.

Another idea for me is that , JSON is called a convenient way to exchanging data, but if this format didn't support pure string,

that is, if I just want to throw out a string, I can't use JSON to do it?

and have to force it change to { Message : "a message"},

and cannot use a way which I think is simpler just throw "a message" ...?

like image 827
yu yang Jian Avatar asked Jan 19 '17 02:01

yu yang Jian


People also ask

Which is not a JSON data type?

JSON values cannot be one of the following data types: a function. a date. undefined.

Is null a JSON primitive?

JSON can represent (sub)values of four primitive data types and of two compound data types. The primitive data types are string, number, boolean, and null.

What is meant by primitive type?

A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values. The eight primitive data types supported by the Java programming language are: byte: The byte data type is an 8-bit signed two's complement integer.


1 Answers

The relevant RFC is RFC 7159, not RFC 4627. RFC 4627 is "informational". RFC 7159 is "standards track"; it explicitly obsoletes RFC 4627.

Request for Comments: 7159                                  Google, Inc.
Obsoletes: 4627, 7158                                         March 2014
Category: Standards Track
ISSN: 2070-1721

In the text of RFC 7159, you'll find this.

13.  Examples

   This is a JSON object:

      {
        "Image": {
            "Width":  800,
            "Height": 600,
            "Title":  "View from 15th Floor",
            "Thumbnail": {
                "Url":    "http://www.example.com/image/481989943",
                "Height": 125,
                "Width":  100
            },
            "Animated" : false,
            "IDs": [116, 943, 234, 38793]
          }
      }
   [snip]
   Here are three small JSON texts containing only values:

   "Hello world!"

   42

   true
like image 144
Mike Sherrill 'Cat Recall' Avatar answered Oct 04 '22 06:10

Mike Sherrill 'Cat Recall'