-
Notifications
You must be signed in to change notification settings - Fork 329
/
Serialization.scala
93 lines (80 loc) · 3.4 KB
/
Serialization.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Copyright 2009-2010 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.json4s
package native
import scala.reflect.Manifest
/** Functions to serialize and deserialize a case class.
* Custom serializer can be inserted if a class is not a case class.
* <p>
* Example:<pre>
* val hints = new ShortTypeHints( ... )
* implicit val formats = Serialization.formats(hints)
* </pre>
*
* @see org.json4s.TypeHints
*/
object Serialization extends Serialization {
import java.io.{Reader, StringWriter, Writer}
/** Serialize to String.
*/
def write[A <: AnyRef](a: A)(implicit formats: Formats): String = {
(write(a, new StringWriter)(formats)).toString
}
/** Serialize to Writer.
*/
def write[A <: AnyRef, W <: Writer](a: A, out: W)(implicit formats: Formats): W = {
Extraction.decomposeWithBuilder(a, JsonWriter.streaming(out)(formats))(formats)
}
/** Serialize to String (pretty format).
*/
def writePretty[A <: AnyRef](a: A)(implicit formats: Formats): String =
(writePretty(a, new StringWriter)(formats)).toString
/** Serialize to Writer (pretty format).
*/
def writePretty[A <: AnyRef, W <: Writer](a: A, out: W)(implicit formats: Formats): W = {
Extraction.decomposeWithBuilder(a, JsonWriter.streamingPretty(out)(formats))(formats)
}
/** Serialize to String (pretty format).
*/
def writePrettyOld[A <: AnyRef](a: A)(implicit formats: Formats): String =
(writePrettyOld(a, new StringWriter)(formats)).toString
/** Serialize to Writer (pretty format).
*/
def writePrettyOld[A <: AnyRef, W <: Writer](a: A, out: W)(implicit formats: Formats): W = {
Printer.pretty(JsonMethods.render(Extraction.decompose(a)(formats)), out)
}
/** Deserialize from a String.
*/
def read[A](json: JsonInput)(implicit formats: Formats, mf: Manifest[A]): A = {
JsonParser.parse(json, formats.wantsBigDecimal, formats.wantsBigInt).extract(formats, mf)
}
@deprecated("You can use formats now to indicate you want to use decimals instead of doubles", "3.2.0")
def read[A](json: String, useBigDecimalForDouble: Boolean)(implicit formats: Formats, mf: Manifest[A]): A =
if (useBigDecimalForDouble) read(json)(formats.withBigDecimal, mf) else read(json)(formats.withDouble, mf)
/** Deserialize from a Reader.
*/
@deprecated("You can use formats now to indicate you want to use decimals instead of doubles", "3.2.0")
def read[A](in: Reader, useBigDecimalForDouble: Boolean)(implicit formats: Formats, mf: Manifest[A]): A =
if (useBigDecimalForDouble) read(in)(formats.withBigDecimal, mf) else read(in)(formats.withDouble, mf)
/** Deserialize from a Reader.
*/
def read[A](in: Reader)(implicit formats: Formats, mf: Manifest[A]): A = {
JsonParser.parse(in,
closeAutomatically = true,
useBigDecimalForDouble = formats.wantsBigDecimal,
useBigIntForLong = formats.wantsBigInt).extract(formats, mf)
}
}