Diferències
Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.
Ambdós costats versió prèvia Revisió prèvia | |||
info:cursos:pue:python-pcpp1:m4:1.3 [08/01/2024 11:29] – mate | info:cursos:pue:python-pcpp1:m4:1.3 [12/01/2024 02:56] (actual) – mate | ||
---|---|---|---|
Línia 22: | Línia 22: | ||
How can we meet such a requirement? | How can we meet such a requirement? | ||
+ | |||
+ | The problem we need to struggle with is **how to represent an object** (understood as a set of data of different types, including other objects) or even a single value in a way that can survive network transfers and inter-platform conversions. | ||
+ | |||
+ | JSON solves the problem using two simple tricks: | ||
+ | |||
+ | * it uses **UTF-8 coded text** – this means that no machine/ | ||
+ | * it uses a simple and not very expanded **format** (we can call it syntax, or even grammar) to represent mutual dependencies and relations between different parts of objects, and is able to transfer not only the values of objects’ properties, but also their names. | ||
+ | |||
+ | In JSON, it can be an unnamed value like a number, a string, a Boolean or… nothing , although this is not what we like most about JSON. JSON is able to carry far more complex data, collected and aggregated in larger compounds. | ||
+ | |||
+ | If you want to transfer not only raw data but also all the names bound to it (like the way in which objects hold their properties), | ||
+ | |||
+ | Yes, we can, but it won't be JSON. If you want your data to be widely understood (not only by Python counterparts), | ||
+ | |||
+ | Let’s start with a very simple example. We want to build a message encapsulating an object containing just one property named prop along with its real (floating-point) value of '' | ||
+ | |||
+ | This is how JSON comes to this: | ||
+ | |||
+ | <code ruby> | ||
+ | { " | ||
+ | </ | ||
+ | |||
+ | Simple? Absolutely! | ||
+ | |||
+ | Now we’re ready to go deeper. Let us show you how JSON represents values of common types. | ||
+ | |||
+ | Note: our presentation is rather comprehensive. If you need more details, refer to the original JSON documentation available here: [[http:// | ||
+ | |||
+ | If you want to encode an integer value (e.g., 123) you will specify this in the following way: | ||
+ | <code json> | ||
+ | 123 | ||
+ | </ | ||
+ | |||
+ | Looks familiar, doesn' | ||
+ | * 0x10 | ||
+ | * 0o10 | ||
+ | * 0b10 | ||
+ | |||
+ | won't be recognized in the JSON environment. Feel free to use a minus sign to put negative numbers inside the JSON text. **Don' | ||
+ | |||
+ | **Real numbers** in JSON are the same as in Python, including utilization of scientific notation. | ||
+ | |||
+ | Here’s an example: | ||
+ | * 3.141592653589 | ||
+ | * 3.0857E16 | ||
+ | * −1.6021766208E−19 | ||
+ | |||
+ | JSON **strings** may look familiar, but there is one important difference – **you must not use apostrophes** to delimit the text. The only delimiter allowed is a quote, like here: | ||
+ | <code ruby> | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | This means that you can’t just insert a quote inside the string – you have to use our old friend backslash (\) followed by a quote instead. | ||
+ | |||
+ | Nothing exciting – but we’re used to that: | ||
+ | <code ruby> | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | Just like in Python, there are some more digraphs and moregraphs (we’re joking, don’t let us confuse you) starting with \ in JSON. We’ve collected all of them here: | ||
+ | {{ : | ||
+ | (**\r** carriage return) | ||
+ | |||
+ | Note that in Python you don’t have to precede / with \. | ||
+ | |||
+ | The '' | ||
+ | |||
+ | Don't forget that JSON strings **cannot be split over multiple lines** – each string must fit entirely on one line (of course, there may be more than one string on the same line). | ||
+ | |||
+ | |||
+ | **Boolean values** are represented (like in Python) by two specific identifiers (the literal name tokens): **true** and **false**: | ||
+ | <code ruby> | ||
+ | true | ||
+ | false | ||
+ | </ | ||
+ | |||
+ | Note: you have to **preserve the case of the literals**, as this is the only acceptable form. | ||
+ | |||
+ | There is one more literal name token in JSON, whose meaning is similar to the one known in Python as None – it may be used to represent no value, or a value without any meaning. | ||
+ | |||
+ | It is called '' | ||
+ | <code ruby> | ||
+ | null | ||
+ | </ | ||
+ | |||
+ | In JSON, all the above values may be combined (or packed) in two ways: | ||
+ | * inside **arrays** (which are a very close relative to Python lists); | ||
+ | * inside **objects** (which resemble Python dictionaries more than objects) | ||
+ | |||
+ | It should be noted that both ways can recursively incorporate any of the two, e.g., a list may contain an object which contains an object which contains a list and so on. | ||
+ | |||
+ | Any JSON object property may contain (or carry) an array. The syntax JSON uses to encode arrays is very similar to the one used by Python to describe lists. For example, it uses square brackets (or just brackets) to delimit array content and uses commas to separate an array' | ||
+ | <code ruby> | ||
+ | [1, 2, 3] | ||
+ | </ | ||
+ | |||
+ | An empty array is denoted simply as a pair of brackets – just like in Python: | ||
+ | <code ruby> | ||
+ | [ ] | ||
+ | </ | ||
+ | |||
+ | Contrary to an array, a JSON object is a set of property specifications surrounded by a pair of braces (curly brackets) – just like here: | ||
+ | <code ruby> | ||
+ | { " | ||
+ | </ | ||
+ | |||
+ | The property specification is a '' | ||
+ | |||
+ | It's worth mentioning that braces are commonly used in all C-derived programming languages and play a role similar to the one known from Python nesting – they mark the boundaries of data definitions or blocks of code. No wonder, then, that they appear in JSON, as JavaScript derives from the C-language too. The similarity to Python dictionary syntax is unintentional. | ||
+ | |||
+ | In this approach, a JSON object is a **set of property specifications separated by commas**. | ||
+ | |||
+ | One important (and very surprising) thing should be stated here. There are no restrictions on property names. No, not at all. These names don't identify variables, so they don’t have to be unique. They don't have to start with a letter. They can even contain a colon, which may seem a little weird at first glance. | ||
+ | |||
+ | This doesn' | ||
+ | |||
+ | |||
+ | If you want to express the fact that a particular **object is empty**, you need to leave the braces and ensure that there is no content between them – just like here: | ||
+ | <code ruby> | ||
+ | { } | ||
+ | </ | ||
+ | |||
+ | When there is more than one property in an object, you can specify all of them in **any order** using commas to separate the items from each other. As JSON ignores white spaces (including tabs) which aren't a part of strings, you can format (or unformat) the text in any way. | ||
+ | |||
+ | For example, both these JSON objects are the same: | ||
+ | <code ruby> | ||
+ | { | ||
+ | x: 123, | ||
+ | y: -1 | ||
+ | } | ||
+ | </ | ||
+ | <code ruby> | ||
+ | {x:123, y:-1} | ||
+ | </ | ||
+ | The first is easier to read (for humans), while the second is cheaper to transmit (it occupies fewer bytes). | ||
+ | |||
+ | Of course, you may incorporate different types of data inside one object: | ||
+ | <code ruby > | ||
+ | { me: " | ||
+ | pi: 3.141592653589, | ||
+ | parsec: 3.0857E16, | ||
+ | electron: −1.6021766208E−19 | ||
+ | friend: " | ||
+ | off: true, | ||
+ | on: false, | ||
+ | set: null } | ||
+ | </ | ||
+ |