or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

advanced-features.mdannotations.mdcore-operations.mddata-structures.mdindex.mdreader-writer.md

core-operations.mddocs/

0

# Core JSON Operations

1

2

Primary JSON parsing and serialization operations using the static JSON interface. Essential for all JSON processing tasks with comprehensive method overloads for different input types and configuration options.

3

4

## Capabilities

5

6

### JSON Parsing

7

8

Parse JSON strings, byte arrays, character arrays, and streams into Java objects with optional feature configuration.

9

10

```java { .api }

11

/**

12

* Parse JSON text to Object

13

* @param text JSON string to parse

14

* @return Parsed object (JSONObject, JSONArray, or primitive)

15

*/

16

public static Object parse(String text);

17

18

/**

19

* Parse JSON text with specific features

20

* @param text JSON string to parse

21

* @param features Reader features to apply

22

* @return Parsed object

23

*/

24

public static Object parse(String text, JSONReader.Feature... features);

25

26

/**

27

* Parse from byte array

28

* @param bytes JSON bytes to parse

29

* @return Parsed object

30

*/

31

public static Object parse(byte[] bytes);

32

33

/**

34

* Parse from character array

35

* @param chars JSON characters to parse

36

* @return Parsed object

37

*/

38

public static Object parse(char[] chars);

39

40

/**

41

* Parse from InputStream

42

* @param inputStream JSON input stream

43

* @return Parsed object

44

*/

45

public static Object parse(InputStream inputStream);

46

```

47

48

### JSON Object Parsing

49

50

Parse JSON specifically to JSONObject instances with type-safe conversion to custom Java classes.

51

52

```java { .api }

53

/**

54

* Parse JSON string to JSONObject

55

* @param text JSON string representing an object

56

* @return JSONObject instance

57

*/

58

public static JSONObject parseObject(String text);

59

60

/**

61

* Parse JSON string to specific Java class

62

* @param text JSON string to parse

63

* @param clazz Target class type

64

* @return Instance of specified class

65

*/

66

public static <T> T parseObject(String text, Class<T> clazz);

67

68

/**

69

* Parse JSON to generic type using TypeReference

70

* @param text JSON string to parse

71

* @param typeReference Type reference for generic types

72

* @return Instance of specified generic type

73

*/

74

public static <T> T parseObject(String text, TypeReference<T> typeReference);

75

76

/**

77

* Parse JSON to Type with features

78

* @param text JSON string to parse

79

* @param type Target type

80

* @return Instance of specified type

81

*/

82

public static <T> T parseObject(String text, Type type);

83

84

/**

85

* Parse from Reader

86

* @param reader JSON character reader

87

* @param clazz Target class type

88

* @return Instance of specified class

89

*/

90

public static <T> T parseObject(Reader reader, Class<T> clazz);

91

92

/**

93

* Parse from URL

94

* @param url JSON resource URL

95

* @param clazz Target class type

96

* @return Instance of specified class

97

*/

98

public static <T> T parseObject(URL url, Class<T> clazz);

99

100

/**

101

* Parse JSON string to Java class with features

102

* @param text JSON string to parse

103

* @param clazz Target class type

104

* @param features Reader features to apply

105

* @return Instance of specified class

106

*/

107

public static <T> T parseObject(String text, Class<T> clazz, JSONReader.Feature... features);

108

109

/**

110

* Parse byte array to specific Java class

111

* @param bytes JSON bytes to parse

112

* @param clazz Target class type

113

* @return Instance of specified class

114

*/

115

public static <T> T parseObject(byte[] bytes, Class<T> clazz);

116

```

117

118

### JSON Array Parsing

119

120

Parse JSON arrays to JSONArray instances or strongly-typed Java collections.

121

122

```java { .api }

123

/**

124

* Parse JSON string to JSONArray

125

* @param text JSON string representing an array

126

* @return JSONArray instance

127

*/

128

public static JSONArray parseArray(String text);

129

130

/**

131

* Parse JSON array to List of specific type

132

* @param text JSON string representing an array

133

* @param clazz Element class type

134

* @return List of specified element type

135

*/

136

public static <T> List<T> parseArray(String text, Class<T> clazz);

137

138

/**

139

* Parse JSON array with features

140

* @param text JSON string representing an array

141

* @param clazz Element class type

142

* @param features Reader features to apply

143

* @return List of specified element type

144

*/

145

public static <T> List<T> parseArray(String text, Class<T> clazz, JSONReader.Feature... features);

146

```

147

148

### JSON Serialization

149

150

Convert Java objects to JSON strings or byte arrays with optional formatting and feature configuration.

151

152

```java { .api }

153

/**

154

* Serialize object to JSON string

155

* @param object Object to serialize

156

* @return JSON string representation

157

*/

158

public static String toJSONString(Object object);

159

160

/**

161

* Serialize object to JSON string with features

162

* @param object Object to serialize

163

* @param features Writer features to apply

164

* @return JSON string representation

165

*/

166

public static String toJSONString(Object object, JSONWriter.Feature... features);

167

168

/**

169

* Serialize object to JSON byte array

170

* @param object Object to serialize

171

* @return JSON bytes

172

*/

173

public static byte[] toJSONBytes(Object object);

174

175

/**

176

* Serialize object to JSON byte array with charset

177

* @param object Object to serialize

178

* @param charset Character encoding

179

* @return JSON bytes in specified encoding

180

*/

181

public static byte[] toJSONBytes(Object object, Charset charset);

182

183

/**

184

* Write object directly to OutputStream

185

* @param outputStream Target output stream

186

* @param object Object to serialize

187

*/

188

public static void writeTo(OutputStream outputStream, Object object);

189

```

190

191

### JSON Validation

192

193

Validate JSON strings for syntax correctness without full parsing.

194

195

```java { .api }

196

/**

197

* Check if text is valid JSON

198

* @param text String to validate

199

* @return true if valid JSON

200

*/

201

public static boolean isValid(String text);

202

203

/**

204

* Check if text is valid JSON object

205

* @param text String to validate

206

* @return true if valid JSON object

207

*/

208

public static boolean isValidObject(String text);

209

210

/**

211

* Check if text is valid JSON array

212

* @param text String to validate

213

* @return true if valid JSON array

214

*/

215

public static boolean isValidArray(String text);

216

```

217

218

### Advanced JSON Validation

219

220

Comprehensive JSON validation with detailed error reporting and type detection.

221

222

```java { .api }

223

/**

224

* JSON validator for advanced validation scenarios

225

*/

226

public class JSONValidator {

227

228

/**

229

* Create validator from JSON string

230

* @param json JSON string to validate

231

* @return JSONValidator instance

232

*/

233

public static JSONValidator from(String json);

234

235

/**

236

* Create validator from byte array

237

* @param bytes JSON bytes to validate

238

* @return JSONValidator instance

239

*/

240

public static JSONValidator from(byte[] bytes);

241

242

/**

243

* Validate JSON structure

244

* @return true if JSON is valid

245

*/

246

public boolean validate();

247

248

/**

249

* Get detected JSON type

250

* @return Type of the JSON content

251

*/

252

public Type getType();

253

}

254

```

255

256

### Configuration and Registration

257

258

Global configuration methods for setting default features and registering custom readers/writers.

259

260

```java { .api }

261

/**

262

* Configure default reader features

263

* @param features Features to enable by default

264

*/

265

public static void config(JSONReader.Feature... features);

266

267

/**

268

* Configure default writer features

269

* @param features Features to enable by default

270

*/

271

public static void config(JSONWriter.Feature... features);

272

273

/**

274

* Register custom ObjectReader for specific type

275

* @param type Target type

276

* @param objectReader Custom reader implementation

277

*/

278

public static void register(Type type, ObjectReader<?> objectReader);

279

280

/**

281

* Register custom ObjectWriter for specific type

282

* @param type Target type

283

* @param objectWriter Custom writer implementation

284

*/

285

public static void register(Type type, ObjectWriter<?> objectWriter);

286

287

/**

288

* Configure mixin classes for additional annotations

289

* @param target Target class

290

* @param mixinSource Mixin class with annotations

291

*/

292

public static void mixIn(Class<?> target, Class<?> mixinSource);

293

294

/**

295

* Copy object with type conversion

296

* @param object Source object

297

* @param targetClass Target class type

298

* @return Converted object instance

299

*/

300

public static <T> T copy(Object object, Class<T> targetClass);

301

302

/**

303

* Copy object to existing target

304

* @param object Source object

305

* @param target Target object to populate

306

*/

307

public static void copyTo(Object object, Object target);

308

309

/**

310

* Parse with offset and length from string

311

* @param text JSON string

312

* @param offset Starting offset

313

* @param length Length to parse

314

* @return Parsed object

315

*/

316

public static Object parse(String text, int offset, int length);

317

318

/**

319

* Parse array with features and filters

320

* @param text JSON string

321

* @param clazz Element class type

322

* @param filter Property filter

323

* @param features Reader features

324

* @return List of specified element type

325

*/

326

public static <T> List<T> parseArray(String text, Class<T> clazz,

327

PropertyFilter filter, JSONReader.Feature... features);

328

```

329

330

**Usage Examples:**

331

332

```java

333

import com.alibaba.fastjson2.JSON;

334

import com.alibaba.fastjson2.JSONReader;

335

import com.alibaba.fastjson2.JSONWriter;

336

337

// Basic parsing

338

String json = "{\"name\":\"John\",\"age\":30}";

339

Object obj = JSON.parse(json);

340

JSONObject jsonObj = JSON.parseObject(json);

341

User user = JSON.parseObject(json, User.class);

342

343

// Array parsing

344

String arrayJson = "[{\"name\":\"John\"},{\"name\":\"Jane\"}]";

345

JSONArray array = JSON.parseArray(arrayJson);

346

List<User> users = JSON.parseArray(arrayJson, User.class);

347

348

// Generic type parsing with TypeReference

349

String mapJson = "{\"users\":{\"john\":{\"name\":\"John\",\"age\":30}}}";

350

Map<String, User> userMap = JSON.parseObject(mapJson, new TypeReference<Map<String, User>>(){});

351

352

List<String> stringList = JSON.parseObject("[\"a\",\"b\",\"c\"]", new TypeReference<List<String>>(){});

353

354

// Complex generic types

355

String complexJson = "{\"data\":[{\"id\":1,\"tags\":[\"tag1\",\"tag2\"]}]}";

356

Map<String, List<DataItem>> complexData = JSON.parseObject(complexJson,

357

new TypeReference<Map<String, List<DataItem>>>(){});

358

359

// Serialization

360

User user = new User("John", 30);

361

String jsonString = JSON.toJSONString(user);

362

String prettyJson = JSON.toJSONString(user, JSONWriter.Feature.PrettyFormat);

363

364

// Validation

365

boolean isValid = JSON.isValid("{\"valid\":true}");

366

boolean isValidObj = JSON.isValidObject("{\"key\":\"value\"}");

367

368

// Advanced validation with type detection

369

JSONValidator validator = JSONValidator.from("{\"name\":\"John\",\"age\":30}");

370

boolean valid = validator.validate();

371

Type detectedType = validator.getType(); // Object type detected

372

373

// Configuration

374

JSON.config(JSONReader.Feature.SupportArrayToBean);

375

JSON.config(JSONWriter.Feature.WriteNulls, JSONWriter.Feature.PrettyFormat);

376

```