Skip to content

Commit 099345f

Browse files
committed
finish js_json migration
1 parent b98336c commit 099345f

File tree

6 files changed

+182
-45
lines changed

6 files changed

+182
-45
lines changed

packages/@rescript/runtime/Js_json.resi

Lines changed: 92 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type rec t = Stdlib_JSON.t =
4444
| Object(dict<t>)
4545
| Array(array<t>)
4646

47+
@deprecated("This functionality has been deprecated and will be removed in v13.")
4748
module Kind: {
4849
type json = t
4950
/** Underlying type of a JSON value */
@@ -56,6 +57,7 @@ module Kind: {
5657
| Null: t<Js_types.null_val>
5758
}
5859

60+
@deprecated("This functionality has been deprecated and will be removed in v13.")
5961
type tagged_t =
6062
| JSONFalse
6163
| JSONTrue
@@ -67,48 +69,67 @@ type tagged_t =
6769

6870
/* ## Accessors */
6971

72+
@deprecated("This functionality has been deprecated and will be removed in v13.")
7073
let classify: t => tagged_t
7174

7275
/**
7376
`test(v, kind)` returns `true` if `v` is of `kind`.
7477
*/
78+
@deprecated("This functionality has been deprecated and will be removed in v13.")
7579
let test: ('a, Kind.t<'b>) => bool
7680

7781
/**
7882
`decodeString(json)` returns `Some(s)` if `json` is a `string`, `None` otherwise.
7983
*/
8084
@deprecated({
81-
reason: "Use pattern matching instead.",
82-
migrate: switch %insert.unlabelledArgument(0) {
83-
| JSON.String(str) => Some(str)
84-
| _ => None
85-
},
85+
reason: "Use `JSON.Decode.string` instead.",
86+
migrate: JSON.Decode.string(),
8687
})
8788
let decodeString: t => option<Js_string.t>
8889

8990
/**
9091
`decodeNumber(json)` returns `Some(n)` if `json` is a `number`, `None` otherwise.
9192
*/
93+
@deprecated({
94+
reason: "Use `JSON.Decode.float` instead.",
95+
migrate: JSON.Decode.float(),
96+
})
9297
let decodeNumber: t => option<float>
9398

9499
/**
95100
`decodeObject(json)` returns `Some(o)` if `json` is an `object`, `None` otherwise.
96101
*/
102+
@deprecated({
103+
reason: "Use `JSON.Decode.object` instead.",
104+
migrate: JSON.Decode.object(),
105+
})
97106
let decodeObject: t => option<dict<t>>
98107

99108
/**
100109
`decodeArray(json)` returns `Some(a)` if `json` is an `array`, `None` otherwise.
101110
*/
111+
@deprecated({
112+
reason: "Use `JSON.Decode.array` instead.",
113+
migrate: JSON.Decode.array(),
114+
})
102115
let decodeArray: t => option<array<t>>
103116

104117
/**
105118
`decodeBoolean(json)` returns `Some(b)` if `json` is a `boolean`, `None` otherwise.
106119
*/
120+
@deprecated({
121+
reason: "Use `JSON.Decode.bool` instead.",
122+
migrate: JSON.Decode.bool(),
123+
})
107124
let decodeBoolean: t => option<bool>
108125

109126
/**
110127
`decodeNull(json)` returns `Some(null)` if `json` is a `null`, `None` otherwise.
111128
*/
129+
@deprecated({
130+
reason: "Use JSON.Decode.null instead.",
131+
migrate: JSON.Decode.null(),
132+
})
112133
let decodeNull: t => option<Js_null.t<'a>>
113134

114135
/* ## Constructors */
@@ -119,22 +140,46 @@ let decodeNull: t => option<Js_null.t<'a>>
119140
*/
120141

121142
/** `null` is the singleton null JSON value. */
143+
@deprecated({
144+
reason: "Use `JSON.Encode.null` instead.",
145+
migrate: JSON.Encode.null,
146+
})
122147
@val
123148
external null: t = "null"
124149

125150
/** `string(s)` makes a JSON string of the `string` `s`. */
151+
@deprecated({
152+
reason: "Use `JSON.Encode.string` instead.",
153+
migrate: JSON.Encode.string(),
154+
})
126155
external string: string => t = "%identity"
127156

128157
/** `number(n)` makes a JSON number of the `float` `n`. */
158+
@deprecated({
159+
reason: "Use `JSON.Encode.float` instead.",
160+
migrate: JSON.Encode.float(),
161+
})
129162
external number: float => t = "%identity"
130163

131164
/** `boolean(b)` makes a JSON boolean of the `bool` `b`. */
165+
@deprecated({
166+
reason: "Use `JSON.Encode.bool` instead.",
167+
migrate: JSON.Encode.bool(),
168+
})
132169
external boolean: bool => t = "%identity"
133170

134171
/** `object_(dict)` makes a JSON object of the `dict`. */
172+
@deprecated({
173+
reason: "Use `JSON.Encode.object` instead.",
174+
migrate: JSON.Encode.object(),
175+
})
135176
external object_: dict<t> => t = "%identity"
136177

137178
/** `array_(a)` makes a JSON array of the `Js.Json.t` array `a`. */
179+
@deprecated({
180+
reason: "Use `JSON.Encode.array` instead.",
181+
migrate: JSON.Encode.array(),
182+
})
138183
external array: array<t> => t = "%identity"
139184

140185
/*
@@ -144,15 +189,31 @@ external array: array<t> => t = "%identity"
144189
*/
145190

146191
/** `stringArray(a)` makes a JSON array of the `string` array `a`. */
192+
@deprecated({
193+
reason: "Use `JSON.Encode.stringArray` instead.",
194+
migrate: JSON.Encode.stringArray(),
195+
})
147196
external stringArray: array<string> => t = "%identity"
148197

149198
/** `numberArray(a)` makes a JSON array of the `float` array `a`. */
199+
@deprecated({
200+
reason: "Use `JSON.Encode.floatArray` instead.",
201+
migrate: JSON.Encode.floatArray(),
202+
})
150203
external numberArray: array<float> => t = "%identity"
151204

152205
/** `booleanArray(a)` makes a JSON array of the `bool` array `a`. */
206+
@deprecated({
207+
reason: "Use `JSON.Encode.boolArray` instead.",
208+
migrate: JSON.Encode.boolArray(),
209+
})
153210
external booleanArray: array<bool> => t = "%identity"
154211

155212
/** `objectArray(a) makes a JSON array of the `JsDict.t` array `a`. */
213+
@deprecated({
214+
reason: "Use `JSON.Encode.objectArray` instead.",
215+
migrate: JSON.Encode.objectArray(),
216+
})
156217
external objectArray: array<dict<t>> => t = "%identity"
157218

158219
/* ## String conversion */
@@ -207,7 +268,12 @@ let getIds = s => {
207268
Js.log(getIds(` { "ids" : [1, 2, 3 ] } `))
208269
```
209270
*/
210-
@val @scope("JSON")
271+
@deprecated({
272+
reason: "Use `JSON.parseOrThrow` instead.",
273+
migrate: JSON.parseOrThrow(),
274+
})
275+
@val
276+
@scope("JSON")
211277
external parseExn: string => t = "parse"
212278

213279
/**
@@ -229,7 +295,12 @@ Js.Dict.set(dict, "likes", Js.Json.stringArray(["ReScript", "ocaml", "js"]))
229295
Js.log(Js.Json.stringify(Js.Json.object_(dict)))
230296
```
231297
*/
232-
@val @scope("JSON")
298+
@deprecated({
299+
reason: "Use `JSON.stringify` instead.",
300+
migrate: JSON.stringify(),
301+
})
302+
@val
303+
@scope("JSON")
233304
external stringify: t => string = "stringify"
234305

235306
/**
@@ -251,7 +322,12 @@ Js.Dict.set(dict, "likes", Js.Json.stringArray(["ReScript", "ocaml", "js"]))
251322
Js.log(Js.Json.stringifyWithSpace(Js.Json.object_(dict), 2))
252323
```
253324
*/
254-
@val @scope("JSON")
325+
@deprecated({
326+
reason: "Use `JSON.stringify` with optional `~space` instead.",
327+
migrate: JSON.stringify(~space=%insert.unlabelledArgument(2)),
328+
})
329+
@val
330+
@scope("JSON")
255331
external stringifyWithSpace: (t, @as(json`null`) _, int) => string = "stringify"
256332

257333
/**
@@ -264,7 +340,12 @@ external stringifyWithSpace: (t, @as(json`null`) _, int) => string = "stringify"
264340
Js.log(Js.Json.stringifyAny(["hello", "world"]))
265341
```
266342
*/
267-
@val @scope("JSON")
343+
@deprecated({
344+
reason: "Use `JSON.stringifyAny` instead.",
345+
migrate: JSON.stringifyAny(),
346+
})
347+
@val
348+
@scope("JSON")
268349
external stringifyAny: 'a => option<string> = "stringify"
269350

270351
/**
@@ -275,6 +356,7 @@ It is unsafe in two aspects
275356
- It may throw during parsing
276357
- when you cast it to a specific type, it may have a type mismatch
277358
*/
359+
@deprecated("This functionality has been deprecated and will be removed in v13.")
278360
let deserializeUnsafe: string => 'a
279361

280362
/**
@@ -283,4 +365,5 @@ It will raise in such situations:
283365
- There are cycles
284366
- Some JS engines can not stringify deeply nested json objects
285367
*/
368+
@deprecated("This functionality has been deprecated and will be removed in v13.")
286369
let serializeExn: 'a => string

packages/@rescript/runtime/Stdlib_JSON.res

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ module Encode = {
8282
external float: float => t = "%identity"
8383
external object: dict<t> => t = "%identity"
8484
external array: array<t> => t = "%identity"
85+
86+
external stringArray: array<string> => t = "%identity"
87+
external floatArray: array<float> => t = "%identity"
88+
external intArray: array<int> => t = "%identity"
89+
external boolArray: array<bool> => t = "%identity"
90+
external objectArray: array<dict<t>> => t = "%identity"
8591
}
8692

8793
module Decode = {

packages/@rescript/runtime/Stdlib_JSON.resi

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ module Classify: {
654654

655655
module Encode: {
656656
/**
657-
Returns a boolean as a JSON object.
657+
Returns a boolean as JSON.
658658
659659
## Examples
660660
```rescript
@@ -664,7 +664,7 @@ module Encode: {
664664
external bool: bool => t = "%identity"
665665

666666
/**
667-
Returns null as a JSON object.
667+
Returns null as a JSON value.
668668
669669
## Examples
670670
```rescript
@@ -674,7 +674,7 @@ module Encode: {
674674
external null: t = "#null"
675675

676676
/**
677-
Returns a string as a JSON object.
677+
Returns a string as JSON.
678678
679679
## Examples
680680
```rescript
@@ -684,7 +684,7 @@ module Encode: {
684684
external string: string => t = "%identity"
685685

686686
/**
687-
Returns an int as a JSON object.
687+
Returns an int as JSON.
688688
689689
## Examples
690690
```rescript
@@ -694,7 +694,7 @@ module Encode: {
694694
external int: int => t = "%identity"
695695

696696
/**
697-
Returns a float as a JSON object.
697+
Returns a float as JSON.
698698
699699
## Examples
700700
```rescript
@@ -719,7 +719,7 @@ module Encode: {
719719
external object: dict<t> => t = "%identity"
720720

721721
/**
722-
Returns an array as a JSON object.
722+
Returns an array as JSON.
723723
724724
## Examples
725725
```rescript
@@ -729,6 +729,31 @@ module Encode: {
729729
```
730730
*/
731731
external array: array<t> => t = "%identity"
732+
733+
/**
734+
Returns an array of strings as a JSON object.
735+
*/
736+
external stringArray: array<string> => t = "%identity"
737+
738+
/**
739+
Returns an array of floats as a JSON object.
740+
*/
741+
external floatArray: array<float> => t = "%identity"
742+
743+
/**
744+
Returns an array of integers as a JSON object.
745+
*/
746+
external intArray: array<int> => t = "%identity"
747+
748+
/**
749+
Returns an array of booleans as a JSON object.
750+
*/
751+
external boolArray: array<bool> => t = "%identity"
752+
753+
/**
754+
Returns an array of objects as a JSON object.
755+
*/
756+
external objectArray: array<dict<t>> => t = "%identity"
732757
}
733758

734759
module Decode: {
Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
external someJson: JSON.t = "someJson"
22
external strToJson: string => JSON.t = "strToJson"
33

4-
let decodeString1 = switch someJson {
5-
| JSON.String(str) => Some(str)
6-
| _ => None
7-
}
8-
let decodeString2 = switch someJson {
9-
| JSON.String(str) => Some(str)
10-
| _ => None
11-
}
12-
let decodeString3 = switch [1, 2, 3]
13-
->Array.map(v => v->Int.toString)
14-
->Array.join(" ")
15-
->strToJson {
16-
| JSON.String(str) => Some(str)
17-
| _ => None
18-
}
4+
let decodeString1 = someJson->JSON.Decode.string
5+
let decodeString2 = JSON.Decode.string(someJson)
6+
let decodeString3 =
7+
[1, 2, 3]->Array.map(v => v->Int.toString)->Array.join(" ")->strToJson->JSON.Decode.string
8+
9+
let decodeNumber1 = someJson->JSON.Decode.float
10+
let decodeNumber2 = JSON.Decode.float(someJson)
11+
12+
let decodeObject1 = someJson->JSON.Decode.object
13+
let decodeObject2 = JSON.Decode.object(someJson)
14+
15+
let decodeArray1 = someJson->JSON.Decode.array
16+
let decodeArray2 = JSON.Decode.array(someJson)
17+
18+
let decodeBoolean1 = someJson->JSON.Decode.bool
19+
let decodeBoolean2 = JSON.Decode.bool(someJson)
20+
21+
let decodeNull1 = someJson->JSON.Decode.null
22+
let decodeNull2 = JSON.Decode.null(someJson)
1923

tests/tools_tests/src/migrate/StdlibMigration_JSON.res

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,18 @@ let decodeString3 =
99
->Array.join(" ")
1010
->strToJson
1111
->Js_json.decodeString
12+
13+
let decodeNumber1 = someJson->Js_json.decodeNumber
14+
let decodeNumber2 = Js_json.decodeNumber(someJson)
15+
16+
let decodeObject1 = someJson->Js_json.decodeObject
17+
let decodeObject2 = Js_json.decodeObject(someJson)
18+
19+
let decodeArray1 = someJson->Js_json.decodeArray
20+
let decodeArray2 = Js_json.decodeArray(someJson)
21+
22+
let decodeBoolean1 = someJson->Js_json.decodeBoolean
23+
let decodeBoolean2 = Js_json.decodeBoolean(someJson)
24+
25+
let decodeNull1 = someJson->Js_json.decodeNull
26+
let decodeNull2 = Js_json.decodeNull(someJson)

0 commit comments

Comments
 (0)