Skip to content

Commit 34082aa

Browse files
committed
Moving Triptoin comments to README
1 parent 6a6184a commit 34082aa

2 files changed

Lines changed: 36 additions & 26 deletions

File tree

README.markdown

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,26 @@ import DefaultJsonProtocol._ // if you don't supply your own Protocol (see below
4141
and do one or more of the following:
4242

4343
* Parse a JSON string into its Abstract Syntax Tree (AST) representation
44-
44+
4545
```scala
4646
val source = """{ "some": "JSON source" }"""
4747
val jsonAst = source.parseJson // or JsonParser(source)
4848
```
49-
49+
5050
* Print a JSON AST back to a String using either the `CompactPrinter` or the `PrettyPrinter`
51-
51+
5252
```scala
5353
val json = jsonAst.prettyPrint // or .compactPrint
5454
```
55-
55+
5656
* Convert any Scala object to a JSON AST using the pimped `toJson` method
57-
57+
5858
```scala
5959
val jsonAst = List(1, 2, 3).toJson
6060
```
61-
61+
6262
* Convert a JSON AST to a Scala object with the `convertTo` method
63-
63+
6464
```scala
6565
val myObject = jsonAst.convertTo[MyObjectType]
6666
```
@@ -95,6 +95,7 @@ important reference and collection types. As long as your code uses nothing more
9595
* String, Symbol
9696
* BigInt, BigDecimal
9797
* Option, Either, Tuple1 - Tuple7
98+
* Tription
9899
* List, Array
99100
* immutable.{Map, Iterable, Seq, IndexedSeq, LinearSeq, Set, Vector}
100101
* collection.{Iterable, Seq, IndexedSeq, LinearSeq, Set}
@@ -103,6 +104,30 @@ important reference and collection types. As long as your code uses nothing more
103104
In most cases however you'll also want to convert types not covered by the `DefaultJsonProtocol`. In these cases you
104105
need to provide `JsonFormat[T]`s for your custom types. This is not hard at all.
105106

107+
### Triptions
108+
109+
`Tription`s are "triple options": values that can either exist, be null, or be undefined.
110+
111+
JavaScript (and JSON), unlike Java/Scala, allow `undefined` values, which are distinct from `null` values.
112+
For example, a PUT request may have a payload like this:
113+
```json
114+
{ "id":"234565434567898789098765",
115+
"field1": "new value",
116+
"field3": null,
117+
"field4": undefined }
118+
```
119+
which would tell the server to update field1 to "new value", set field3 to null, and leave field2 and field4
120+
unchanged. With a standard scala `Option`, it is impossible to tell whether the values of field2, field3,
121+
and field4 in the original payload were `null` or `undefined` since any missing values translate to `None`.
122+
123+
The `Tription` solves that problem by defining `Value` for present values, `Null` for null values, and
124+
`Undefined` for values which are missing or explicitly marked as undefined.
125+
126+
`Tription`s can be used just like `Option`s:
127+
```scala
128+
case class RequestObject( id: String, field1: Tription[String], field2: Tription[Int],
129+
field3: Tription[String], field4: Tription[SubResource] )
130+
```
106131

107132
### Providing JsonFormats for Case Classes
108133

@@ -158,10 +183,10 @@ object MyJsonProtocol extends DefaultJsonProtocol {
158183
#### NullOptions
159184

160185
The `NullOptions` trait supplies an alternative rendering mode for optional case class members. Normally optional
161-
members that are undefined (`None`) are not rendered at all. By mixing in this trait into your custom JsonProtocol you
186+
members that are undefined (`None`/`Undefined`) are not rendered at all. By mixing in this trait into your custom JsonProtocol you
162187
can enforce the rendering of undefined members as `null`.
163-
(Note that this only affect JSON writing, spray-json will always read missing optional members as well as `null`
164-
optional members as `None`.)
188+
(Note that this only affect JSON writing, spray-json will always read missing `Option` members as well as `null`
189+
`Option` members as `None` and missing `Tription` members as `Undefined`.)
165190

166191

167192
### Providing JsonFormats for other Types

src/main/scala/spray/json/Tription.scala

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
11
package spray.json
22

33
/**
4-
* A Triple-Option
5-
*
6-
* JavaScript (and JSON), unlike Java/Scala, allow undefined values, which are distinct from null values.
7-
* For example, a PUT request may have a payload like this:
8-
* <code>
9-
* { "id":"234565434567898789098765",
10-
* "field1": 7,
11-
* "field3: null,
12-
* "field4": undefined }
13-
* </code>
14-
* which would tell the server to update field1 to 7, set field3 to null, and leave field2 and field4 alone.
15-
* With a standard scala `Option`, it is impossible to tell whether the payload of the request had field2, field3,
16-
* and field4 null or undefined since any missing values translate to `None`.
17-
*
18-
* The Tription solves that problem by defining `Value` for present values, `Null` for null values, and
19-
* `Undefined` for values which are missing or explicitly marked as undefined.
4+
* A Triple-Option for JSON values: `Undefined`, `Null`, or `Value(x)` See readme for more details
205
*
216
* Created by bathalh on 2/19/16.
227
*/

0 commit comments

Comments
 (0)