Skip to content

Commit 28ea1a8

Browse files
committed
Add JSON number
1 parent 69917f9 commit 28ea1a8

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ j := jsn.O{
3030
"do": jsn.A{
3131
"go", "mod", "tidy",
3232
},
33+
"x": jsn.N("123456.00000000000000000000000000000000000000001"),
3334
}
3435

3536
raw, _ := json.MarshalIndent(j, "", " ")
@@ -42,7 +43,8 @@ fmt.Printf("%s\n", raw)
4243
// "mod",
4344
// "tidy"
4445
// ],
45-
// "hello": "world"
46+
// "hello": "world",
47+
// "x": 123456.00000000000000000000000000000000000000001
4648
//}
4749
```
4850

example_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import (
77
"github.com/cristalhq/jsn"
88
)
99

10-
func ExampleUsage() {
10+
func Example() {
1111
j := jsn.O{
1212
"hello": "world",
1313
"do": jsn.A{
1414
"go", "mod", "tidy",
1515
},
16+
"x": jsn.N("123456.00000000000000000000000000000000000000001"),
1617
}
1718

1819
raw, _ := json.MarshalIndent(j, "", " ")
@@ -25,6 +26,25 @@ func ExampleUsage() {
2526
// "mod",
2627
// "tidy"
2728
// ],
28-
// "hello": "world"
29+
// "hello": "world",
30+
// "x": 123456.00000000000000000000000000000000000000001
31+
//}
32+
}
33+
34+
func ExampleNumber() {
35+
j := jsn.O{
36+
"x": 123456.00000000000000000000000000000000000000001,
37+
"y": jsn.N("123456.00000000000000000000000000000000000000001"),
38+
"z": "123456.00000000000000000000000000000000000000001",
39+
}
40+
41+
raw, _ := json.MarshalIndent(j, "", " ")
42+
fmt.Printf("%s\n", raw)
43+
44+
// Output:
45+
//{
46+
// "x": 123456,
47+
// "y": 123456.00000000000000000000000000000000000000001,
48+
// "z": "123456.00000000000000000000000000000000000000001"
2949
//}
3050
}

jsn.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@ type A = []any
55

66
// O represents JSON object.
77
type O = map[string]any
8+
9+
// N represents JSON number.
10+
type N string
11+
12+
func (n N) MarshalJSON() ([]byte, error) {
13+
return []byte(n), nil
14+
}

0 commit comments

Comments
 (0)