2
2
3
3
import java .io .IOException ;
4
4
import java .util .Arrays ;
5
- import java .util .Map ;
6
5
import java .util .List ;
6
+ import java .util .Map ;
7
7
8
8
import com .jsoniter .output .JsonStream ;
9
9
import com .jsoniter .output .JsonStreamPool ;
@@ -15,7 +15,7 @@ private JsonSerializer() {
15
15
}
16
16
17
17
/**
18
- * Serialize an instance into a JSON object and return it as a byte array.
18
+ * Serialize an instance into a byte array.
19
19
*
20
20
* @param obj the instance
21
21
* @return the byte array
@@ -28,19 +28,31 @@ public static byte[] serialize(Object obj) {
28
28
return Arrays .copyOfRange (stream .buffer ().data (), 0 , stream .buffer ().tail ());
29
29
} catch (IOException e ) {
30
30
throw new JsonException (e );
31
- } finally {
32
- JsonStreamPool .returnJsonStream (stream );
33
31
}
34
32
}
35
33
36
34
/**
37
- * Serialize a map of strings into a JSON object and return it as a byte array.
35
+ * Serialize an instance into a JSON stream.
36
+ *
37
+ * @param obj the instance
38
+ * @param stream the JSON stream
39
+ */
40
+ public static void serialize (Object obj , JsonStream stream ) {
41
+ try {
42
+ stream .reset (null );
43
+ stream .writeVal (obj .getClass (), obj );
44
+ } catch (IOException e ) {
45
+ throw new JsonException (e );
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Serialize a map of strings into a JSON stream.
38
51
*
39
52
* @param map the map
40
- * @return the byte array
53
+ * @param stream the JSON stream
41
54
*/
42
- public static byte [] serialize (Map <String , String > map ) {
43
- JsonStream stream = JsonStreamPool .borrowJsonStream ();
55
+ public static void serialize (Map <String , String > map , JsonStream stream ) {
44
56
try {
45
57
stream .reset (null );
46
58
stream .writeObjectStart ();
@@ -53,22 +65,18 @@ public static byte[] serialize(Map<String, String> map) {
53
65
}
54
66
});
55
67
stream .writeObjectEnd ();
56
- return Arrays .copyOfRange (stream .buffer ().data (), 0 , stream .buffer ().tail ());
57
68
} catch (IOException e ) {
58
69
throw new JsonException (e );
59
- } finally {
60
- JsonStreamPool .returnJsonStream (stream );
61
70
}
62
71
}
63
72
64
73
/**
65
- * Serialize a list of objects into a JSON array and return it as a byte array .
74
+ * Serialize a list of objects into a JSON stream .
66
75
*
67
76
* @param objs the list of objects
68
- * @return the byte array
77
+ * @param stream the JSON stream
69
78
*/
70
- public static byte [] serialize (List <?> objs ) {
71
- JsonStream stream = JsonStreamPool .borrowJsonStream ();
79
+ public static void serialize (List <?> objs , JsonStream stream ) {
72
80
try {
73
81
stream .reset (null );
74
82
stream .writeArrayStart ();
@@ -82,11 +90,8 @@ public static byte[] serialize(List<?> objs) {
82
90
83
91
}
84
92
stream .writeArrayEnd ();
85
- return Arrays .copyOfRange (stream .buffer ().data (), 0 , stream .buffer ().tail ());
86
93
} catch (IOException e ) {
87
94
throw new JsonException (e );
88
- } finally {
89
- JsonStreamPool .returnJsonStream (stream );
90
95
}
91
96
}
92
97
}
0 commit comments