@@ -34,9 +34,9 @@ let parseDifficulty = (s: string): PuzzleFormat.difficulty => {
3434}
3535
3636// Parse tier from JSON (either int or string)
37- let parseTier = (json : Js . Json .t ): PuzzleFormat .tier => {
38- switch Js . Json .classify (json ) {
39- | JSONNumber (n ) =>
37+ let parseTier = (json : JSON .t ): PuzzleFormat .tier => {
38+ switch JSON . Classify .classify (json ) {
39+ | Number (n ) =>
4040 switch Belt .Float .toInt (n ) {
4141 | 0 => Tier0
4242 | 1 => Tier1
@@ -45,7 +45,7 @@ let parseTier = (json: Js.Json.t): PuzzleFormat.tier => {
4545 | 4 => Tier4
4646 | _ => Tier0
4747 }
48- | JSONString (s ) =>
48+ | String (s ) =>
4949 switch String .toLowerCase (s ) {
5050 | "tier0" | "0" => Tier0
5151 | "tier1" | "1" => Tier1
@@ -95,15 +95,15 @@ let inferTierFromInstructions = (instrs: array<string>): PuzzleFormat.tier => {
9595}
9696
9797// Extract register pairs from a state object (JSON dict of string -> int)
98- let extractRegisters = (stateJson : Js . Json .t ): array <(string , int )> => {
99- switch Js . Json .classify (stateJson ) {
100- | JSONObject (dict ) =>
98+ let extractRegisters = (stateJson : JSON .t ): array <(string , int )> => {
99+ switch JSON . Classify .classify (stateJson ) {
100+ | Object (dict ) =>
101101 let keys = Dict .keysToArray (dict )
102102 Belt .Array .keepMap (keys , key => {
103103 switch Dict .get (dict , key ) {
104104 | Some (v ) =>
105- switch Js . Json .classify (v ) {
106- | JSONNumber (n ) => Some ((key , Belt .Float .toInt (n )))
105+ switch JSON . Classify .classify (v ) {
106+ | Number (n ) => Some ((key , Belt .Float .toInt (n )))
107107 | _ => None
108108 }
109109 | None => None
@@ -114,34 +114,34 @@ let extractRegisters = (stateJson: Js.Json.t): array<(string, int)> => {
114114}
115115
116116// Parse hints from either old format [{moveNumber, text}] or new [string]
117- let parseHints = (json : Js . Json .t ): array <PuzzleFormat .hint > => {
118- switch Js . Json .classify (json ) {
119- | JSONArray (arr ) =>
117+ let parseHints = (json : JSON .t ): array <PuzzleFormat .hint > => {
118+ switch JSON . Classify .classify (json ) {
119+ | Array (arr ) =>
120120 Belt .Array .keepMap (arr , item => {
121- switch Js . Json .classify (item ) {
121+ switch JSON . Classify .classify (item ) {
122122 // New format: array of strings
123- | JSONString (s ) => Some ({PuzzleFormat .moveThreshold : 3 , text : s })
123+ | String (s ) => Some ({PuzzleFormat .moveThreshold : 3 , text : s })
124124 // Old format: {moveNumber, text} objects
125- | JSONObject (dict ) => {
125+ | Object (dict ) => {
126126 let text = switch Dict .get (dict , "text" ) {
127127 | Some (v ) =>
128- switch Js . Json .classify (v ) {
129- | JSONString (s ) => s
128+ switch JSON . Classify .classify (v ) {
129+ | String (s ) => s
130130 | _ => ""
131131 }
132132 | None => ""
133133 }
134134 let threshold = switch Dict .get (dict , "moveNumber" ) {
135135 | Some (v ) =>
136- switch Js . Json .classify (v ) {
137- | JSONNumber (n ) => Belt .Float .toInt (n )
136+ switch JSON . Classify .classify (v ) {
137+ | Number (n ) => Belt .Float .toInt (n )
138138 | _ => 3
139139 }
140140 | None =>
141141 switch Dict .get (dict , "moveThreshold" ) {
142142 | Some (v ) =>
143- switch Js . Json .classify (v ) {
144- | JSONNumber (n ) => Belt .Float .toInt (n )
143+ switch JSON . Classify .classify (v ) {
144+ | Number (n ) => Belt .Float .toInt (n )
145145 | _ => 3
146146 }
147147 | None => 3
@@ -161,38 +161,38 @@ let parseHints = (json: Js.Json.t): array<PuzzleFormat.hint> => {
161161}
162162
163163// Get a string field from a JSON object
164- let getString = (dict : dict <Js . Json .t >, key : string , default : string ): string => {
164+ let getString = (dict : dict <JSON .t >, key : string , default : string ): string => {
165165 switch Dict .get (dict , key ) {
166166 | Some (v ) =>
167- switch Js . Json .classify (v ) {
168- | JSONString (s ) => s
167+ switch JSON . Classify .classify (v ) {
168+ | String (s ) => s
169169 | _ => default
170170 }
171171 | None => default
172172 }
173173}
174174
175175// Get an int field from a JSON object
176- let getInt = (dict : dict <Js . Json .t >, key : string , default : int ): int => {
176+ let getInt = (dict : dict <JSON .t >, key : string , default : int ): int => {
177177 switch Dict .get (dict , key ) {
178178 | Some (v ) =>
179- switch Js . Json .classify (v ) {
180- | JSONNumber (n ) => Belt .Float .toInt (n )
179+ switch JSON . Classify .classify (v ) {
180+ | Number (n ) => Belt .Float .toInt (n )
181181 | _ => default
182182 }
183183 | None => default
184184 }
185185}
186186
187187// Get a string array from a JSON object
188- let getStringArray = (dict : dict <Js . Json .t >, key : string ): array <string > => {
188+ let getStringArray = (dict : dict <JSON .t >, key : string ): array <string > => {
189189 switch Dict .get (dict , key ) {
190190 | Some (v ) =>
191- switch Js . Json .classify (v ) {
192- | JSONArray (arr ) =>
191+ switch JSON . Classify .classify (v ) {
192+ | Array (arr ) =>
193193 Belt .Array .keepMap (arr , item => {
194- switch Js . Json .classify (item ) {
195- | JSONString (s ) => Some (s )
194+ switch JSON . Classify .classify (item ) {
195+ | String (s ) => Some (s )
196196 | _ => None
197197 }
198198 })
@@ -205,16 +205,16 @@ let getStringArray = (dict: dict<Js.Json.t>, key: string): array<string> => {
205205// Parse a single puzzle JSON string into PuzzleFormat.t
206206// id: the puzzle identifier (typically filename without .json)
207207let parsePuzzleJson = (id : string , jsonStr : string ): option <PuzzleFormat .t > => {
208- switch Js . Json . parseExn (jsonStr )-> Js . Json .classify {
209- | JSONObject (dict ) => {
208+ switch JSON . parseExn (jsonStr )-> JSON . Classify .classify {
209+ | Object (dict ) => {
210210 let name = getString (dict , "name" , id )
211211 let description = getString (dict , "description" , "" )
212212 let difficulty = getString (dict , "difficulty" , "beginner" )-> parseDifficulty
213213 let maxMoves = getInt (dict , "maxMoves" , 50 )
214214 let parMoves = switch Dict .get (dict , "optimalMoves" ) {
215215 | Some (v ) =>
216- switch Js . Json .classify (v ) {
217- | JSONNumber (n ) => Belt .Float .toInt (n )
216+ switch JSON . Classify .classify (v ) {
217+ | Number (n ) => Belt .Float .toInt (n )
218218 | _ => getInt (dict , "parMoves" , maxMoves )
219219 }
220220 | None => getInt (dict , "parMoves" , maxMoves )
@@ -284,8 +284,8 @@ let parsePuzzleJson = (id: string, jsonStr: string): option<PuzzleFormat.t> => {
284284 } else {
285285 switch Dict .get (dict , "metadata" ) {
286286 | Some (v ) =>
287- switch Js . Json .classify (v ) {
288- | JSONObject (metaDict ) => getStringArray (metaDict , "tags" )
287+ switch JSON . Classify .classify (v ) {
288+ | Object (metaDict ) => getStringArray (metaDict , "tags" )
289289 | _ => []
290290 }
291291 | None => []
@@ -315,36 +315,36 @@ let parsePuzzleJson = (id: string, jsonStr: string): option<PuzzleFormat.t> => {
315315}
316316
317317// Serialize a puzzle to JSON (for bundle generation)
318- let puzzleToJson = (p : PuzzleFormat .t ): Js . Json .t => {
318+ let puzzleToJson = (p : PuzzleFormat .t ): JSON .t => {
319319 let dict = Dict .make ()
320- Dict .set (dict , "id" , Js . Json .string (p .id ))
321- Dict .set (dict , "name" , Js . Json .string (p .name ))
322- Dict .set (dict , "description" , Js . Json .string (p .description ))
323- Dict .set (dict , "difficulty" , Js . Json .string (PuzzleFormat .difficultyToString (p .difficulty )))
324- Dict .set (dict , "tier" , Js . Json . number (Belt .Int .toFloat (PuzzleFormat .tierToInt (p .tier ))))
325- Dict .set (dict , "maxMoves" , Js . Json . number (Belt .Int .toFloat (p .maxMoves )))
326- Dict .set (dict , "parMoves" , Js . Json . number (Belt .Int .toFloat (p .parMoves )))
320+ Dict .set (dict , "id" , JSON . Encode .string (p .id ))
321+ Dict .set (dict , "name" , JSON . Encode .string (p .name ))
322+ Dict .set (dict , "description" , JSON . Encode .string (p .description ))
323+ Dict .set (dict , "difficulty" , JSON . Encode .string (PuzzleFormat .difficultyToString (p .difficulty )))
324+ Dict .set (dict , "tier" , JSON . Encode . float (Belt .Int .toFloat (PuzzleFormat .tierToInt (p .tier ))))
325+ Dict .set (dict , "maxMoves" , JSON . Encode . float (Belt .Int .toFloat (p .maxMoves )))
326+ Dict .set (dict , "parMoves" , JSON . Encode . float (Belt .Int .toFloat (p .parMoves )))
327327
328328 // Registers as objects
329- let regsToJson = (regs : array <(string , int )>): Js . Json .t => {
329+ let regsToJson = (regs : array <(string , int )>): JSON .t => {
330330 let d = Dict .make ()
331331 Belt .Array .forEach (regs , ((k , v )) => {
332- Dict .set (d , k , Js . Json . number (Belt .Int .toFloat (v )))
332+ Dict .set (d , k , JSON . Encode . float (Belt .Int .toFloat (v )))
333333 })
334- Js . Json . object_ (d )
334+ JSON . Encode . object (d )
335335 }
336336 Dict .set (dict , "initialRegisters" , regsToJson (p .initialRegisters ))
337337 Dict .set (dict , "goalRegisters" , regsToJson (p .goalRegisters ))
338338
339339 // Memory pairs
340340 if Belt .Array .length (p .initialMemory ) > 0 {
341- let memToJson = (pairs : array <(int , int )>): Js . Json .t => {
342- Js . Json .array (
341+ let memToJson = (pairs : array <(int , int )>): JSON .t => {
342+ JSON . Encode .array (
343343 Belt .Array .map (pairs , ((addr , val_ )) => {
344344 let d = Dict .make ()
345- Dict .set (d , "addr" , Js . Json . number (Belt .Int .toFloat (addr )))
346- Dict .set (d , "value" , Js . Json . number (Belt .Int .toFloat (val_ )))
347- Js . Json . object_ (d )
345+ Dict .set (d , "addr" , JSON . Encode . float (Belt .Int .toFloat (addr )))
346+ Dict .set (d , "value" , JSON . Encode . float (Belt .Int .toFloat (val_ )))
347+ JSON . Encode . object (d )
348348 }),
349349 )
350350 }
@@ -355,7 +355,7 @@ let puzzleToJson = (p: PuzzleFormat.t): Js.Json.t => {
355355 // Allowed instructions
356356 switch p .allowedInstructions {
357357 | Some (instrs ) =>
358- Dict .set (dict , "allowedInstructions" , Js . Json .array (Belt .Array .map (instrs , Js . Json .string )))
358+ Dict .set (dict , "allowedInstructions" , JSON . Encode .array (Belt .Array .map (instrs , JSON . Encode .string )))
359359 | None => ()
360360 }
361361
@@ -364,31 +364,31 @@ let puzzleToJson = (p: PuzzleFormat.t): Js.Json.t => {
364364 Dict .set (
365365 dict ,
366366 "hints" ,
367- Js . Json .array (
367+ JSON . Encode .array (
368368 Belt .Array .map (p .hints , h => {
369369 let d = Dict .make ()
370- Dict .set (d , "moveThreshold" , Js . Json . number (Belt .Int .toFloat (h .moveThreshold )))
371- Dict .set (d , "text" , Js . Json .string (h .text ))
372- Js . Json . object_ (d )
370+ Dict .set (d , "moveThreshold" , JSON . Encode . float (Belt .Int .toFloat (h .moveThreshold )))
371+ Dict .set (d , "text" , JSON . Encode .string (h .text ))
372+ JSON . Encode . object (d )
373373 }),
374374 ),
375375 )
376376 }
377377
378378 // Tags
379379 if Belt .Array .length (p .tags ) > 0 {
380- Dict .set (dict , "tags" , Js . Json .array (Belt .Array .map (p .tags , Js . Json .string )))
380+ Dict .set (dict , "tags" , JSON . Encode .array (Belt .Array .map (p .tags , JSON . Encode .string )))
381381 }
382382
383- Js . Json . object_ (dict )
383+ JSON . Encode . object (dict )
384384}
385385
386386// Serialize an array of puzzles to a bundle JSON string
387387let bundleToJsonString = (puzzles : array <PuzzleFormat .t >): string => {
388388 let bundle = Dict .make ()
389- Dict .set (bundle , "version" , Js . Json .string ("1.0.0" ))
390- Dict .set (bundle , "generatedAt" , Js . Json .string ("build-time" ))
391- Dict .set (bundle , "count" , Js . Json . number (Belt .Int .toFloat (Belt .Array .length (puzzles ))))
392- Dict .set (bundle , "puzzles" , Js . Json .array (Belt .Array .map (puzzles , puzzleToJson )))
393- Js . Json . stringify (Js . Json . object_ (bundle ))
389+ Dict .set (bundle , "version" , JSON . Encode .string ("1.0.0" ))
390+ Dict .set (bundle , "generatedAt" , JSON . Encode .string ("build-time" ))
391+ Dict .set (bundle , "count" , JSON . Encode . float (Belt .Int .toFloat (Belt .Array .length (puzzles ))))
392+ Dict .set (bundle , "puzzles" , JSON . Encode .array (Belt .Array .map (puzzles , puzzleToJson )))
393+ JSON . stringify (JSON . Encode . object (bundle ))
394394}
0 commit comments