1EWXIVMRK%VHYMRS.WSR - ArduinoJson: Efficient JSON ...

BENOIT BLANCHON

CREATOR OF ARDUINOJSON

Mastering ArduinoJson 6

Efficient JSON serialization for embedded C++

THIRD EDITION

Contents

Contents

iv

1 Introduction

1.1 About this book . . . . . . . . . . . . . . . . . .

1.1.1 Overview . . . . . . . . . . . . . . . . . .

1.1.2 Code samples . . . . . . . . . . . . . . .

1.1.3 What¡¯s new in the third edition . . . . . .

1.2 Introduction to JSON . . . . . . . . . . . . . . .

1.2.1 What is JSON? . . . . . . . . . . . . . .

1.2.2 What is serialization? . . . . . . . . . . .

1.2.3 What can you do with JSON? . . . . . .

1.2.4 History of JSON . . . . . . . . . . . . . .

1.2.5 Why is JSON so popular? . . . . . . . . .

1.2.6 The JSON syntax . . . . . . . . . . . . .

1.2.7 Binary data in JSON . . . . . . . . . . .

1.2.8 Comments in JSON . . . . . . . . . . . .

1.3 Introduction to ArduinoJson . . . . . . . . . . .

1.3.1 What ArduinoJson is . . . . . . . . . . .

1.3.2 What ArduinoJson is not . . . . . . . . .

1.3.3 What makes ArduinoJson different? . . .

1.3.4 Does size matter? . . . . . . . . . . . . .

1.3.5 What are the alternatives to ArduinoJson?

1.3.6 How to install ArduinoJson . . . . . . . .

1.3.7 The examples . . . . . . . . . . . . . . .

1.4 Summary . . . . . . . . . . . . . . . . . . . . . .

2 The

2.1

2.2

2.3

missing C++ course

Why a C++ course? . . . . . . . . . . .

Harvard and von Neumann architectures

Stack, heap, and globals . . . . . . . .

2.3.1 Globals . . . . . . . . . . . . . .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1

2

2

2

3

4

4

5

5

7

8

9

12

13

14

14

14

15

17

18

20

25

27

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

28

29

31

33

34

Contents

2.4

2.5

2.6

2.7

2.8

v

2.3.2 Heap . . . . . . . . . . . . . . .

2.3.3 Stack . . . . . . . . . . . . . . .

Pointers . . . . . . . . . . . . . . . . .

2.4.1 What is a pointer? . . . . . . .

2.4.2 Dereferencing a pointer . . . . .

2.4.3 Pointers and arrays . . . . . . .

2.4.4 Taking the address of a variable

2.4.5 Pointer to class and struct . .

2.4.6 Pointer to constant . . . . . . .

2.4.7 The null pointer . . . . . . . . .

2.4.8 Why use pointers? . . . . . . . .

Memory management . . . . . . . . . .

2.5.1 malloc() and free() . . . . . . .

2.5.2 new and delete . . . . . . . . .

2.5.3 Smart pointers . . . . . . . . . .

2.5.4 RAII . . . . . . . . . . . . . . .

References . . . . . . . . . . . . . . . .

2.6.1 What is a reference? . . . . . .

2.6.2 Differences with pointers . . . .

2.6.3 Reference to constant . . . . . .

2.6.4 Rules of references . . . . . . .

2.6.5 Common problems . . . . . . .

2.6.6 Usage for references . . . . . . .

Strings . . . . . . . . . . . . . . . . . .

2.7.1 How are the strings stored? . . .

2.7.2 String literals in RAM . . . . . .

2.7.3 String literals in Flash . . . . . .

2.7.4 Pointer to the ¡°globals¡± section .

2.7.5 Mutable string in ¡°globals¡± . . .

2.7.6 A copy in the stack . . . . . . .

2.7.7 A copy in the heap . . . . . . .

2.7.8 A word about the String class .

2.7.9 Pass strings to functions . . . .

Summary . . . . . . . . . . . . . . . . .

3 Deserialize with ArduinoJson

3.1 The example of this chapter . . . . .

3.2 Deserializing an object . . . . . . . . .

3.2.1 The JSON document . . . . .

3.2.2 Placing the JSON document in

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

35

36

38

38

38

39

40

40

41

43

44

45

45

45

46

48

49

49

49

50

51

51

52

53

53

53

54

56

56

57

58

59

60

63

. . . . .

. . . . .

. . . . .

memory

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

65

66

67

67

67

Contents

3.3

3.4

3.5

3.6

3.7

3.8

3.9

vi

3.2.3 Introducing JsonDocument . . . . . . . . . . . .

3.2.4 How to specify the capacity? . . . . . . . . . .

3.2.5 How to determine the capacity? . . . . . . . .

3.2.6 StaticJsonDocument or DynamicJsonDocument? . .

3.2.7 Deserializing the JSON document . . . . . . .

Extracting values from an object . . . . . . . . . . . .

3.3.1 Extracting values . . . . . . . . . . . . . . . .

3.3.2 Explicit casts . . . . . . . . . . . . . . . . . .

3.3.3 When values are missing . . . . . . . . . . . .

3.3.4 Changing the default value . . . . . . . . . . .

Inspecting an unknown object . . . . . . . . . . . . . .

3.4.1 Getting a reference to the object . . . . . . . .

3.4.2 Enumerating the keys . . . . . . . . . . . . . .

3.4.3 Detecting the type of value . . . . . . . . . . .

3.4.4 Variant types and C++ types . . . . . . . . . .

3.4.5 Testing if a key exists in an object . . . . . . .

Deserializing an array . . . . . . . . . . . . . . . . . .

3.5.1 The JSON document . . . . . . . . . . . . . .

3.5.2 Parsing the array . . . . . . . . . . . . . . . .

3.5.3 The ArduinoJson Assistant . . . . . . . . . . .

Extracting values from an array . . . . . . . . . . . . .

3.6.1 Retrieving elements by index . . . . . . . . . .

3.6.2 Alternative syntaxes . . . . . . . . . . . . . . .

3.6.3 When complex values are missing . . . . . . . .

Inspecting an unknown array . . . . . . . . . . . . . .

3.7.1 Getting a reference to the array . . . . . . . . .

3.7.2 Capacity of JsonDocument for an unknown input

3.7.3 Number of elements in an array . . . . . . . .

3.7.4 Iteration . . . . . . . . . . . . . . . . . . . . .

3.7.5 Detecting the type of an element . . . . . . . .

The zero-copy mode . . . . . . . . . . . . . . . . . . .

3.8.1 Definition . . . . . . . . . . . . . . . . . . . .

3.8.2 An example . . . . . . . . . . . . . . . . . . .

3.8.3 Input buffer must stay in memory . . . . . . .

Reading from read-only memory . . . . . . . . . . . .

3.9.1 The example . . . . . . . . . . . . . . . . . . .

3.9.2 Duplication is required . . . . . . . . . . . . .

3.9.3 Practice . . . . . . . . . . . . . . . . . . . . .

3.9.4 Other types of read-only input . . . . . . . . .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

68

68

69

70

70

72

72

72

73

74

75

75

76

76

77

78

79

79

79

81

83

83

83

84

86

86

86

87

87

88

90

90

90

92

93

93

93

94

95

Contents

3.10 Reading from a stream . . . .

3.10.1 Reading from a file . .

3.10.2 Reading from an HTTP

3.11 Summary . . . . . . . . . . . .

vii

. . . . .

. . . . .

response

. . . . .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

. 97

. 97

. 98

. 106

4 Serializing with ArduinoJson

4.1 The example of this chapter . . . . . . . . .

4.2 Creating an object . . . . . . . . . . . . . . .

4.2.1 The example . . . . . . . . . . . . . .

4.2.2 Allocating the JsonDocument . . . . .

4.2.3 Adding members . . . . . . . . . . .

4.2.4 Alternative syntax . . . . . . . . . . .

4.2.5 Creating an empty object . . . . . . .

4.2.6 Removing members . . . . . . . . . .

4.2.7 Replacing members . . . . . . . . . .

4.3 Creating an array . . . . . . . . . . . . . . .

4.3.1 The example . . . . . . . . . . . . . .

4.3.2 Allocating the JsonDocument . . . . . .

4.3.3 Adding elements . . . . . . . . . . . .

4.3.4 Adding nested objects . . . . . . . . .

4.3.5 Creating an empty array . . . . . . .

4.3.6 Replacing elements . . . . . . . . . .

4.3.7 Removing elements . . . . . . . . . .

4.4 Writing to memory . . . . . . . . . . . . . .

4.4.1 Minified JSON . . . . . . . . . . . . .

4.4.2 Specifying (or not) the buffer size . .

4.4.3 Prettified JSON . . . . . . . . . . . .

4.4.4 Measuring the length . . . . . . . . .

4.4.5 Writing to a String . . . . . . . . . .

4.4.6 Casting a JsonVariant to a String . .

4.5 Writing to a stream . . . . . . . . . . . . . .

4.5.1 What¡¯s an output stream? . . . . . .

4.5.2 Writing to the serial port . . . . . . .

4.5.3 Writing to a file . . . . . . . . . . . .

4.5.4 Writing to a TCP connection . . . . .

4.6 Duplication of strings . . . . . . . . . . . . .

4.6.1 An example . . . . . . . . . . . . . .

4.6.2 Keys and values . . . . . . . . . . . .

4.6.3 Copy only occurs when adding values

4.6.4 ArduinoJson Assistant to the rescue .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

108

109

110

110

110

111

111

112

112

113

114

114

114

115

115

116

116

117

118

118

118

119

120

121

121

122

122

123

124

124

129

129

130

130

131

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download