Skip to content

Commit 02efde4

Browse files
committed
adjust ArcTable json parsing according to changes in ArcTable structure
1 parent 8bdd981 commit 02efde4

2 files changed

Lines changed: 49 additions & 23 deletions

File tree

src/Json/Decode.fs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,20 @@ module Decode =
196196

197197
else
198198
("", BadPrimitive("an object", value)) |> Error
199+
}
200+
201+
let tryOneOf (decoders : Decoder<'value> list) : Decoder<'value> =
202+
{ new Decoder<'value> with
203+
member _.Decode(helpers, value) =
204+
let rec loop (errors : DecoderError<'JsonValue> list) (decoders : Decoder<'value> list) =
205+
match decoders with
206+
| [] -> Error ("", BadOneOf errors)
207+
| decoder :: rest ->
208+
let decodingResult =
209+
try decoder.Decode(helpers, value)
210+
with e -> Error (DecoderError("", ErrorReason.FailMessage e.Message))
211+
match decodingResult with
212+
| Ok v -> Ok v
213+
| Error e -> loop (e :: errors) rest
214+
loop [] decoders
199215
}

src/Json/Table/ArcTable.fs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ open System.Collections.Generic
88
module ArcTable =
99

1010
let encoder (table: ArcTable) =
11-
let keyEncoder : Encoder<int*int> = Encode.tuple2 Encode.int Encode.int
11+
let valueMap = table.Values.ValueMap
12+
let cellEncoder : Encoder<int> = fun hash -> CompositeCell.encoder valueMap.[hash]
1213
let columnEncoder (col : ArcTableAux.ColumnValueRefs) : IEncodable =
1314
match col with
14-
| ArcTableAux.ColumnValueRefs.Constant hash -> Encode.int hash
15-
| ArcTableAux.ColumnValueRefs.Sparse cells -> Encode.intDictionary Encode.int cells
15+
| ArcTableAux.ColumnValueRefs.Constant hash -> cellEncoder hash
16+
| ArcTableAux.ColumnValueRefs.Sparse cells -> Encode.intDictionary cellEncoder cells
1617

1718
Encode.object [
1819
"name", Encode.string table.Name
@@ -21,7 +22,6 @@ module ArcTable =
2122
for h in table.Headers do yield CompositeHeader.encoder h
2223
]
2324
if table.Values.RowCount <> 0 then
24-
"cells", Encode.intDictionary CompositeCell.encoder table.Values.ValueMap
2525
"columns", Encode.intDictionary columnEncoder table.Values.Columns
2626
"rowCount", Encode.int table.RowCount
2727
]
@@ -44,20 +44,24 @@ module ArcTable =
4444
)
4545

4646
let decoder : Decoder<ArcTable> =
47-
47+
let valueMap = Dictionary<int, CompositeCell>()
48+
let cellDecoder : Decoder<int> =
49+
CompositeCell.decoder
50+
|> Decode.map (fun cell ->
51+
ArcTableAux.ensureCellHashInValueMap cell valueMap
52+
)
4853
let columnDecoder : Decoder<ArcTableAux.ColumnValueRefs> =
49-
Decode.oneOf [
50-
Decode.int |> Decode.map ArcTableAux.ColumnValueRefs.Constant
51-
Decode.dictionary Decode.int Decode.int |> Decode.map ArcTableAux.ColumnValueRefs.Sparse
54+
Decode.tryOneOf [
55+
cellDecoder |> Decode.map ArcTableAux.ColumnValueRefs.Constant
56+
Decode.intDictionary cellDecoder |> Decode.map ArcTableAux.ColumnValueRefs.Sparse
5257
]
5358
let decoder : Decoder<ArcTable> =
5459
Decode.object(fun get ->
5560
let decodedHeader = get.Optional.Field "headers" (Decode.resizeArray CompositeHeader.decoder) |> Option.defaultValue (ResizeArray())
56-
let decodedCells = get.Optional.Field "cells" (Decode.dictionary Decode.int CompositeCell.decoder) |> Option.defaultValue (Dictionary())
5761
let decodedColumns = get.Optional.Field "columns" (Decode.dictionary Decode.int columnDecoder) |> Option.defaultValue (Dictionary())
5862
let rowCount = get.Optional.Field "rowCount" Decode.int |> Option.defaultValue 0
5963

60-
let values = ArcTableAux.ArcTableValues(decodedColumns, decodedCells, rowCount)
64+
let values = ArcTableAux.ArcTableValues(decodedColumns, valueMap, rowCount)
6165

6266
ArcTable.fromArcTableValues(
6367
get.Required.Field "name" Decode.string,
@@ -134,20 +138,20 @@ module ArcTable =
134138
open StringTable
135139

136140
let encoderCompressed (stringTable : StringTableMap) (oaTable : OATableMap) (cellTable : CellTableMap) (table: ArcTable) =
137-
let keyEncoder : Encoder<int*int> = Encode.tuple2 Encode.int Encode.int
141+
let cellEncoder (hash : int) =
142+
CellTable.encodeCell cellTable (table.Values.ValueMap.[hash])
138143
let columnEncoder (col : ArcTableAux.ColumnValueRefs) : IEncodable =
139144
match col with
140-
| ArcTableAux.ColumnValueRefs.Constant hash -> Encode.int hash
141-
| ArcTableAux.ColumnValueRefs.Sparse cells -> Encode.intDictionary Encode.int cells
145+
| ArcTableAux.ColumnValueRefs.Constant hash -> cellEncoder hash
146+
| ArcTableAux.ColumnValueRefs.Sparse cells -> Encode.intDictionary cellEncoder cells
142147
Encode.object [
143148
"n", StringTable.encodeString stringTable table.Name
144149
if table.Headers.Count <> 0 then
145150
"h", Encode.list [
146151
for h in table.Headers do yield CompositeHeader.encoder h
147152
]
148153
if table.Values.RowCount <> 0 then
149-
"ce", Encode.intDictionary (CellTable.encodeCell cellTable) table.Values.ValueMap
150-
"co", Encode.intDictionary columnEncoder table.Values.Columns
154+
"c", Encode.intDictionary columnEncoder table.Values.Columns
151155
"r", Encode.int table.RowCount
152156
]
153157

@@ -170,20 +174,26 @@ module ArcTable =
170174
)
171175

172176
let decoderCompressed (stringTable : StringTableArray) (oaTable : OATableArray) (cellTable : CellTableArray) : Decoder<ArcTable> =
173-
177+
let valueMap = Dictionary<int, CompositeCell>()
178+
let cellDecoder : Decoder<int> =
179+
Decode.int
180+
|> Decode.map (fun i ->
181+
let cell = cellTable.[i]
182+
ArcTableAux.ensureCellHashInValueMap cell valueMap
183+
)
174184
let columnDecoder : Decoder<ArcTableAux.ColumnValueRefs> =
175-
Decode.oneOf [
176-
Decode.int |> Decode.map ArcTableAux.ColumnValueRefs.Constant
177-
Decode.dictionary Decode.int Decode.int |> Decode.map ArcTableAux.ColumnValueRefs.Sparse
185+
Decode.tryOneOf [
186+
cellDecoder |> Decode.map ArcTableAux.ColumnValueRefs.Constant
187+
Decode.intDictionary cellDecoder |> Decode.map ArcTableAux.ColumnValueRefs.Sparse
178188
]
179189
let decoder : Decoder<ArcTable> =
180190
Decode.object(fun get ->
181191
let decodedHeader = get.Optional.Field "h" (Decode.resizeArray CompositeHeader.decoder) |> Option.defaultValue (ResizeArray())
182-
let decodedCells = get.Optional.Field "ce" (Decode.dictionary Decode.int (CellTable.decodeCell cellTable)) |> Option.defaultValue (Dictionary())
183-
let decodedColumns = get.Optional.Field "co" (Decode.dictionary Decode.int columnDecoder) |> Option.defaultValue (Dictionary())
192+
193+
let decodedColumns = get.Optional.Field "c" (Decode.dictionary Decode.int columnDecoder) |> Option.defaultValue (Dictionary())
184194
let rowCount = get.Optional.Field "r" Decode.int |> Option.defaultValue 0
185195

186-
let values = ArcTableAux.ArcTableValues(decodedColumns, decodedCells, rowCount)
196+
let values = ArcTableAux.ArcTableValues(decodedColumns, valueMap, rowCount)
187197

188198
ArcTable.fromArcTableValues(
189199
get.Required.Field "n" (StringTable.decodeString stringTable),
@@ -193,7 +203,7 @@ module ArcTable =
193203
)
194204
{new Decoder<ArcTable> with
195205
member this.Decode (helper, column) =
196-
if helper.hasProperty "c" column then
206+
if helper.hasProperty "r" column |> not then
197207
// This is the old format, we need to decode it with the old decoder
198208
(decoderCompressedV2Deprecated stringTable oaTable cellTable).Decode(helper, column)
199209
else

0 commit comments

Comments
 (0)