Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add json column representation #94

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/FsSpreadsheet.Js/FsExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ type FsWorkbook with



static member fromJsonString (json:string) : FsWorkbook =
Json.fromJsonString json
static member fromRowsJsonString (json:string) : FsWorkbook =
Json.fromRowsJsonString json

static member toJsonString (wb:FsWorkbook) : string =
Json.toJsonString wb
static member toRowsJsonString (wb:FsWorkbook) : string =
Json.toRowsJsonString wb

//static member fromJsonFile (path:string) : Promise<FsWorkbook> =
// Json.fromJsonFile path
Expand All @@ -55,5 +55,15 @@ type FsWorkbook with
//member this.ToJsonFile(path: string) : Promise<unit> =
// FsWorkbook.toJsonFile path this

member this.ToJsonString() : string =
FsWorkbook.toJsonString this
member this.ToRowsJsonString() : string =
FsWorkbook.toRowsJsonString this


static member fromColumnsJsonString (json:string) : FsWorkbook =
Json.fromColumnsJsonString json

static member toColumnsJsonString (wb:FsWorkbook) : string =
Json.toColumnsJsonString wb

member this.ToColumnsJsonString() : string =
FsWorkbook.toColumnsJsonString this
27 changes: 21 additions & 6 deletions src/FsSpreadsheet.Js/Json.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,34 @@ open Thoth.Json.JavaScript
[<AttachMembers>]
type Json =

static member tryFromJsonString (json:string) : Result<FsWorkbook, string> =
match Thoth.Json.JavaScript.Decode.fromString FsSpreadsheet.Json.Workbook.decode json with
static member tryFromRowsJsonString (json:string) : Result<FsWorkbook, string> =
match Thoth.Json.JavaScript.Decode.fromString FsSpreadsheet.Json.Workbook.decodeRows json with
| Ok wb -> Ok wb
| Error e -> Error e

static member fromJsonString (json:string) : FsWorkbook =
match Json.tryFromJsonString json with
static member fromRowsJsonString (json:string) : FsWorkbook =
match Json.tryFromRowsJsonString json with
| Ok wb -> wb
| Error e -> failwithf "Could not deserialize json Workbook: \n%s" e

static member toJsonString (wb:FsWorkbook, ?spaces) : string =
static member toRowsJsonString (wb:FsWorkbook, ?spaces) : string =
let spaces = defaultArg spaces 2
FsSpreadsheet.Json.Workbook.encode wb
FsSpreadsheet.Json.Workbook.encodeRows wb
|> Thoth.Json.JavaScript.Encode.toString spaces

static member tryFromColumnsJsonString (json:string) : Result<FsWorkbook, string> =
match Thoth.Json.JavaScript.Decode.fromString FsSpreadsheet.Json.Workbook.decodeColumns json with
| Ok wb -> Ok wb
| Error e -> Error e

static member fromColumnsJsonString (json:string) : FsWorkbook =
match Json.tryFromColumnsJsonString json with
| Ok wb -> wb
| Error e -> failwithf "Could not deserialize json Workbook: \n%s" e

static member toColumnsJsonString (wb:FsWorkbook, ?spaces) : string =
let spaces = defaultArg spaces 2
FsSpreadsheet.Json.Workbook.encodeColumns wb
|> Thoth.Json.JavaScript.Encode.toString spaces

//static member fromJsonFile (path:string) : Promise<FsWorkbook> =
Expand Down
85 changes: 65 additions & 20 deletions src/FsSpreadsheet.Net/FsExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -330,30 +330,59 @@ module FsExtensions =
/// <summary>
/// Takes a json string and returns the FsWorkbook based on its content.
/// </summary>
static member tryFromJsonString (json : string) =
Thoth.Json.Newtonsoft.Decode.fromString FsSpreadsheet.Json.Workbook.decode json
static member tryFromRowsJsonString (json : string) =
Thoth.Json.Newtonsoft.Decode.fromString FsSpreadsheet.Json.Workbook.decodeRows json

/// <summary>
/// Takes a json string and returns the FsWorkbook based on its content.
/// </summary>
static member fromJsonString (json : string) =
match FsWorkbook.tryFromJsonString json with
static member fromRowsJsonString (json : string) =
match FsWorkbook.tryFromRowsJsonString json with
| Ok wb -> wb
| Error e -> failwithf "Could not deserialize json Workbook: \n%s" e

/// <summary>
/// Takes the path to an json file and returns the FsWorkbook based on its content.
/// </summary>
static member tryFromJsonFile (filePath : string) =
static member tryFromRowsJsonFile (filePath : string) =
let json = File.ReadAllText filePath
FsWorkbook.tryFromJsonString json
FsWorkbook.tryFromRowsJsonString json

/// <summary>
/// Takes the path to an json file and returns the FsWorkbook based on its content.
/// </summary>
static member fromJsonFile (filePath : string) =
static member fromRowsJsonFile (filePath : string) =
let json = File.ReadAllText filePath
FsWorkbook.fromJsonString json
FsWorkbook.fromRowsJsonString json

/// <summary>
/// Takes a json string and returns the FsWorkbook based on its content.
/// </summary>
static member tryFromColumnsJsonString (json : string) =
Thoth.Json.Newtonsoft.Decode.fromString FsSpreadsheet.Json.Workbook.decodeColumns json

/// <summary>
/// Takes a json string and returns the FsWorkbook based on its content.
/// </summary>
static member fromColumnsJsonString (json : string) =
match FsWorkbook.tryFromColumnsJsonString json with
| Ok wb -> wb
| Error e -> failwithf "Could not deserialize json Workbook: \n%s" e

/// <summary>
/// Takes the path to an json file and returns the FsWorkbook based on its content.
/// </summary>
static member tryFromColumnsJsonFile (filePath : string) =
let json = File.ReadAllText filePath
FsWorkbook.tryFromColumnsJsonString json

/// <summary>
/// Takes the path to an json file and returns the FsWorkbook based on its content.
/// </summary>
static member fromColumnsJsonFile (filePath : string) =
let json = File.ReadAllText filePath
FsWorkbook.fromColumnsJsonString json


member self.ToEmptySpreadsheet(doc : Packaging.SpreadsheetDocument) =

Expand Down Expand Up @@ -428,21 +457,37 @@ module FsExtensions =
static member toFile (filePath : string) path (workbook : FsWorkbook) =
workbook.ToXlsxFile(path)

static member toJsonString (workbook : FsWorkbook, ?spaces) =
static member toRowsJsonString (workbook : FsWorkbook, ?spaces) =
let spaces = defaultArg spaces 2
FsSpreadsheet.Json.Workbook.encodeRows workbook
|> Thoth.Json.Newtonsoft.Encode.toString spaces

static member toRowsJsonFile (path, ?spaces) =
fun workbook ->
let json = FsWorkbook.toRowsJsonString (workbook,?spaces = spaces)
File.WriteAllText(path,json)

member this.ToRowsJsonString(?spaces) =
FsWorkbook.toRowsJsonString(this, ?spaces = spaces)

member this.ToRowsJsonFile(path: string, ?spaces) =
FsWorkbook.toRowsJsonFile(path, ?spaces = spaces) this

static member toColumnsJsonString (workbook : FsWorkbook, ?spaces) =
let spaces = defaultArg spaces 2
FsSpreadsheet.Json.Workbook.encode workbook
FsSpreadsheet.Json.Workbook.encodeColumns workbook
|> Thoth.Json.Newtonsoft.Encode.toString spaces

static member toJsonFile (path, ?spaces) =
static member toColumnsJsonFile (path, ?spaces) =
fun workbook ->
let json = FsWorkbook.toJsonString (workbook,?spaces = spaces)
let json = FsWorkbook.toColumnsJsonString (workbook,?spaces = spaces)
File.WriteAllText(path,json)

member this.ToJsonString(?spaces) =
FsWorkbook.toJsonString(this, ?spaces = spaces)
member this.ToColumnsJsonString(?spaces) =
FsWorkbook.toColumnsJsonString(this, ?spaces = spaces)

member this.ToJsonFile(path: string, ?spaces) =
FsWorkbook.toJsonFile(path, ?spaces = spaces) this
member this.ToColumnsJsonFile(path: string, ?spaces) =
FsWorkbook.toColumnsJsonFile(path, ?spaces = spaces) this

type Writer =

Expand Down Expand Up @@ -474,8 +519,8 @@ type Writer =
static member toFile(path,workbook: FsWorkbook) =
workbook.ToXlsxFile(path)

static member toJsonString (workbook : FsWorkbook, ?spaces) =
FsWorkbook.toJsonString(workbook, ?spaces = spaces)
static member toRowsJsonString (workbook : FsWorkbook, ?spaces) =
FsWorkbook.toRowsJsonString(workbook, ?spaces = spaces)

static member toJsonFile (path, ?spaces) =
fun workbook -> FsWorkbook.toJsonFile (path, ?spaces = spaces) workbook
static member toRowsJsonFile (path, ?spaces) =
fun workbook -> FsWorkbook.toRowsJsonFile (path, ?spaces = spaces) workbook
22 changes: 16 additions & 6 deletions src/FsSpreadsheet.Py/FsExtension.fs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ type FsWorkbook with
member this.ToXlsxBytes() : byte [] =
FsWorkbook.toXlsxBytes this

static member fromJsonString (json:string) : FsWorkbook =
Json.fromJsonString json
static member fromRowsJsonString (json:string) : FsWorkbook =
Json.fromRowsJsonString json

static member toJsonString (wb:FsWorkbook) : string =
Json.toJsonString wb
static member toRowsJsonString (wb:FsWorkbook) : string =
Json.toRowsJsonString wb

//static member fromJsonFile (path:string) : Promise<FsWorkbook> =
// Json.fromJsonFile path
Expand All @@ -51,5 +51,15 @@ type FsWorkbook with
//member this.ToJsonFile(path: string) : Promise<unit> =
// FsWorkbook.toJsonFile path this

member this.ToJsonString() : string =
FsWorkbook.toJsonString this
member this.ToRowsJsonString() : string =
FsWorkbook.toRowsJsonString this

static member fromColumnsJsonString (json:string) : FsWorkbook =
Json.fromColumnsJsonString json

static member toColumnsJsonString (wb:FsWorkbook) : string =
Json.toColumnsJsonString wb

member this.ToColumnsJsonString() : string =
FsWorkbook.toColumnsJsonString this

25 changes: 19 additions & 6 deletions src/FsSpreadsheet.Py/Json.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,28 @@ open Thoth.Json.Python
[<AttachMembers>]
type Json =

static member tryFromJsonString (json:string) : Result<FsWorkbook, string> =
Decode.fromString FsSpreadsheet.Json.Workbook.decode json
static member tryFromRowsJsonString (json:string) : Result<FsWorkbook, string> =
Decode.fromString FsSpreadsheet.Json.Workbook.decodeRows json

static member fromJsonString (json:string) : FsWorkbook =
match Json.tryFromJsonString json with
static member fromRowsJsonString (json:string) : FsWorkbook =
match Json.tryFromRowsJsonString json with
| Ok wb -> wb
| Error e -> failwithf "Could not deserialize json Workbook: \n%s" e

static member toJsonString (wb:FsWorkbook, ?spaces) : string =
static member toRowsJsonString (wb:FsWorkbook, ?spaces) : string =
let spaces = defaultArg spaces 2
FsSpreadsheet.Json.Workbook.encode wb
FsSpreadsheet.Json.Workbook.encodeRows wb
|> Encode.toString spaces

static member tryFromColumnsJsonString (json:string) : Result<FsWorkbook, string> =
Decode.fromString FsSpreadsheet.Json.Workbook.decodeColumns json

static member fromColumnsJsonString (json:string) : FsWorkbook =
match Json.tryFromColumnsJsonString json with
| Ok wb -> wb
| Error e -> failwithf "Could not deserialize json Workbook: \n%s" e

static member toColumnsJsonString (wb:FsWorkbook, ?spaces) : string =
let spaces = defaultArg spaces 2
FsSpreadsheet.Json.Workbook.encodeColumns wb
|> Encode.toString spaces
5 changes: 2 additions & 3 deletions src/FsSpreadsheet/FsSpreadsheet.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<Compile Include="Json\Value.fs" />
<Compile Include="Json\Cell.fs" />
<Compile Include="Json\Row.fs" />
<Compile Include="Json\Column.fs" />
<Compile Include="Json\Table.fs" />
<Compile Include="Json\Worksheet.fs" />
<Compile Include="Json\Workbook.fs" />
Expand All @@ -57,8 +58,6 @@
<PackageReference Include="Thoth.Json.Core" Version="0.2.1" />
</ItemGroup>

<ItemGroup>
<Content Include="*.fsproj; **\*.fs; **\*.fsi" PackagePath="fable\" />
</ItemGroup>
<ItemGroup />
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deleted fable files !!!


</Project>
21 changes: 19 additions & 2 deletions src/FsSpreadsheet/Json/Cell.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,35 @@ open Thoth.Json.Core
[<Literal>]
let column = "column"

[<Literal>]
let row = "row"

[<Literal>]
let value = "value"

let encode (cell:FsCell) =
let encodeRows (cell:FsCell) =
Encode.object [
column, Encode.int cell.ColumnNumber
value, Value.encode cell.Value
]

let decode rowNumber : Decoder<FsCell> =
let decodeRows rowNumber : Decoder<FsCell> =
Decode.object (fun builder ->
let v,dt = builder.Required.Field value (Value.decode)
let c = builder.Required.Field column Decode.int
new FsCell(v,dt,FsAddress(rowNumber,c))
)


let encodeCols (cell:FsCell) =
Encode.object [
row, Encode.int cell.RowNumber
value, Value.encode cell.Value
]

let decodeCols colNumber : Decoder<FsCell> =
Decode.object (fun builder ->
let v,dt = builder.Required.Field value (Value.decode)
let r = builder.Required.Field row Decode.int
new FsCell(v,dt,FsAddress(r,colNumber))
)
23 changes: 23 additions & 0 deletions src/FsSpreadsheet/Json/Column.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module FsSpreadsheet.Json.Column

open FsSpreadsheet
open Thoth.Json.Core

[<Literal>]
let cells = "cells"

[<Literal>]
let number = "number"

let encode (col:FsColumn) =
Encode.object [
number, Encode.int col.Index
cells, Encode.seq (col.Cells |> Seq.map Cell.encodeCols)
]

let decode : Decoder<int*FsCell seq> =
Decode.object (fun builder ->
let n = builder.Required.Field number Decode.int
let cs = builder.Required.Field cells (Decode.seq (Cell.decodeCols n))
n,cs
)
4 changes: 2 additions & 2 deletions src/FsSpreadsheet/Json/Row.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ let number = "number"
let encode (row:FsRow) =
Encode.object [
number, Encode.int row.Index
cells, Encode.seq (row.Cells |> Seq.map Cell.encode)
cells, Encode.seq (row.Cells |> Seq.map Cell.encodeRows)
]

let decode : Decoder<int*FsCell seq> =
Decode.object (fun builder ->
let n = builder.Required.Field number Decode.int
let cs = builder.Required.Field cells (Decode.seq (Cell.decode n))
let cs = builder.Required.Field cells (Decode.seq (Cell.decodeRows n))
n,cs
)
Loading
Loading