or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

exception-handling.mdfactory-configuration.mdfeatures-configuration.mdindex.mdjson-generation.mdjson-parsing.mdutilities-advanced.md

factory-configuration.mddocs/

0

# Factory and Configuration

1

2

JsonFactory serves as the central factory for creating JsonParser and JsonGenerator instances. It provides thread-safe, reusable configuration for JSON processing with extensive customization options.

3

4

## Factory Creation

5

6

### Builder Pattern (Recommended)

7

8

```java { .api }

9

public static JsonFactoryBuilder builder() {

10

return new JsonFactoryBuilder();

11

}

12

13

public static class JsonFactoryBuilder extends TSFBuilder<JsonFactory, JsonFactoryBuilder> {

14

public JsonFactoryBuilder enable(JsonFactory.Feature f);

15

public JsonFactoryBuilder disable(JsonFactory.Feature f);

16

public JsonFactoryBuilder enable(StreamReadFeature f);

17

public JsonFactoryBuilder disable(StreamReadFeature f);

18

public JsonFactoryBuilder enable(StreamWriteFeature f);

19

public JsonFactoryBuilder disable(StreamWriteFeature f);

20

public JsonFactoryBuilder streamReadConstraints(StreamReadConstraints src);

21

public JsonFactoryBuilder streamWriteConstraints(StreamWriteConstraints swc);

22

public JsonFactoryBuilder errorReportConfiguration(ErrorReportConfiguration erc);

23

public JsonFactory build();

24

}

25

```

26

27

### Legacy Constructor

28

29

```java { .api }

30

public JsonFactory() {

31

this((ObjectCodec) null);

32

}

33

34

public JsonFactory(ObjectCodec codec) {

35

this(codec, DEFAULT_FACTORY_FEATURE_FLAGS);

36

}

37

```

38

39

## Parser Creation

40

41

```java { .api }

42

public JsonParser createParser(String content) throws IOException;

43

public JsonParser createParser(InputStream in) throws IOException;

44

public JsonParser createParser(Reader r) throws IOException;

45

public JsonParser createParser(byte[] data) throws IOException;

46

public JsonParser createParser(byte[] data, int offset, int len) throws IOException;

47

public JsonParser createParser(File f) throws IOException;

48

public JsonParser createParser(URL url) throws IOException;

49

public JsonParser createParser(DataInput in) throws IOException;

50

```

51

52

## Generator Creation

53

54

```java { .api }

55

public JsonGenerator createGenerator(OutputStream out) throws IOException;

56

public JsonGenerator createGenerator(OutputStream out, JsonEncoding enc) throws IOException;

57

public JsonGenerator createGenerator(Writer w) throws IOException;

58

public JsonGenerator createGenerator(File f, JsonEncoding enc) throws IOException;

59

public JsonGenerator createGenerator(DataOutput out) throws IOException;

60

```

61

62

## Factory Features

63

64

### Core Factory Features

65

66

```java { .api }

67

public enum Feature implements JacksonFeature {

68

// Symbol handling

69

INTERN_FIELD_NAMES(true),

70

CANONICALIZE_FIELD_NAMES(true),

71

FAIL_ON_SYMBOL_HASH_OVERFLOW(true),

72

73

// Parser features

74

USE_FAST_DOUBLE_PARSER(true),

75

USE_FAST_BIG_NUMBER_PARSER(true),

76

77

// Generator features

78

USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING(true);

79

80

public boolean enabledByDefault();

81

public boolean enabledIn(int flags);

82

public int getMask();

83

}

84

```

85

86

## Stream Constraints

87

88

### Read Constraints

89

90

```java { .api }

91

public final class StreamReadConstraints implements Serializable {

92

public static final int DEFAULT_MAX_STRING_LEN = 20_000_000;

93

public static final int DEFAULT_MAX_NUMBER_LEN = 1000;

94

public static final int DEFAULT_MAX_DEPTH = 1000;

95

public static final long DEFAULT_MAX_DOC_LEN = -1L;

96

public static final int DEFAULT_MAX_NAME_LEN = 50000;

97

98

public static Builder builder();

99

public static StreamReadConstraints defaults();

100

101

public int getMaxStringLength();

102

public int getMaxNumberLength();

103

public int getMaxNestingDepth();

104

public long getMaxDocumentLength();

105

public int getMaxNameLength();

106

107

public static class Builder {

108

public Builder maxStringLength(int maxStringLength);

109

public Builder maxNumberLength(int maxNumberLength);

110

public Builder maxNestingDepth(int maxNestingDepth);

111

public Builder maxDocumentLength(long maxDocumentLength);

112

public Builder maxNameLength(int maxNameLength);

113

public StreamReadConstraints build();

114

}

115

}

116

```

117

118

### Write Constraints

119

120

```java { .api }

121

public final class StreamWriteConstraints implements Serializable {

122

public static final int DEFAULT_MAX_DEPTH = 1000;

123

124

public static Builder builder();

125

public static StreamWriteConstraints defaults();

126

127

public int getMaxNestingDepth();

128

129

public static class Builder {

130

public Builder maxNestingDepth(int maxNestingDepth);

131

public StreamWriteConstraints build();

132

}

133

}

134

```

135

136

## Error Report Configuration

137

138

```java { .api }

139

public final class ErrorReportConfiguration implements Serializable {

140

public static final int DEFAULT_MAX_RAW_CONTENT_LENGTH = 500;

141

public static final int DEFAULT_MAX_ERROR_TOKEN_LENGTH = 256;

142

143

public static Builder builder();

144

public static ErrorReportConfiguration defaults();

145

146

public int getMaxRawContentLength();

147

public int getMaxErrorTokenLength();

148

149

public static class Builder {

150

public Builder maxRawContentLength(int maxRawContentLength);

151

public Builder maxErrorTokenLength(int maxErrorTokenLength);

152

public ErrorReportConfiguration build();

153

}

154

}

155

```

156

157

## Usage Examples

158

159

### Basic Factory Setup

160

161

```java

162

JsonFactory factory = JsonFactory.builder()

163

.enable(JsonReadFeature.ALLOW_JAVA_COMMENTS)

164

.enable(JsonReadFeature.ALLOW_SINGLE_QUOTES)

165

.disable(JsonFactory.Feature.INTERN_FIELD_NAMES)

166

.build();

167

```

168

169

### With Constraints

170

171

```java

172

JsonFactory factory = JsonFactory.builder()

173

.streamReadConstraints(StreamReadConstraints.builder()

174

.maxStringLength(1_000_000)

175

.maxNumberLength(100)

176

.maxNestingDepth(500)

177

.build())

178

.streamWriteConstraints(StreamWriteConstraints.builder()

179

.maxNestingDepth(500)

180

.build())

181

.build();

182

```

183

184

### With Error Configuration

185

186

```java

187

JsonFactory factory = JsonFactory.builder()

188

.errorReportConfiguration(ErrorReportConfiguration.builder()

189

.maxErrorTokenLength(200)

190

.maxRawContentLength(1000)

191

.build())

192

.build();

193

```

194

195

### Legacy Configuration

196

197

```java

198

JsonFactory factory = new JsonFactory();

199

factory.enable(JsonReadFeature.ALLOW_JAVA_COMMENTS);

200

factory.disable(JsonFactory.Feature.INTERN_FIELD_NAMES);

201

```

202

203

## Thread Safety

204

205

JsonFactory instances are thread-safe once configured and can be reused across multiple threads. Configuration should be done before sharing the factory instance between threads.

206

207

## Buffer Management

208

209

Jackson Core uses buffer recycling for improved performance. The factory manages buffer pools through the BufferRecycler system, which can be configured through factory features.

210

211

```java { .api }

212

public BufferRecycler _getBufferRecycler();

213

public void _releaseBuffers(BufferRecycler recycler);

214

```