Skip to content

Commit 52a379b

Browse files
committed
Update README and examples
1 parent fb05417 commit 52a379b

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

README.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ kind of hints see [`json-typedef-infer`][jtd-infer].
1212

1313
## Usage
1414

15-
See [examples] directory for runnable examples and how to infer JTD.
15+
See [examples] directory for runnable examples and how to infer JTD. If you want
16+
to infer a primitive Go type such as a `string` or `map[string]any`, you can use
17+
`Infer`:
1618

1719
```go
1820
schema := NewInferrer(WithoutHints()).
@@ -23,8 +25,29 @@ schema := NewInferrer(WithoutHints()).
2325
// }
2426
```
2527

26-
If you have multiple rows of objects or lists as strings you can pass them to
27-
the shorthand function `InferStrings`.
28+
```go
29+
schema := NewInferrer(WithoutHints()).
30+
Infer(map[string]any{
31+
"age": 52,
32+
"name": "Joe",
33+
}).
34+
IntoSchema()
35+
// {
36+
// "properties": {
37+
// "age": {
38+
// "type": "uint8"
39+
// },
40+
// "name": {
41+
// "type": "string"
42+
// }
43+
// }
44+
// }
45+
```
46+
47+
If you have one or more rows of JSON objects or lists as strings you can pass
48+
them to the shorthand function `InferStrings`. This will create an `Inferrer`
49+
and call `Infer` repeatedly on each row after deserializing it to a Go map. This
50+
is the recommended way to infer the schema even if you just have a single line.
2851

2952
```go
3053
rows := []string{

examples/main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
func main() {
1111
inferSimpleValue()
1212
inferMultipleStringRows()
13+
inferMap()
14+
inferManualUnmarshal()
1315
inferWithHints()
1416
}
1517

@@ -24,6 +26,20 @@ func inferSimpleValue() {
2426
fmt.Println()
2527
}
2628

29+
func inferMap() {
30+
schema := jtdinfer.
31+
NewInferrer(jtdinfer.WithoutHints()).
32+
Infer(map[string]any{
33+
"age": 52,
34+
"name": "Joe",
35+
}).
36+
IntoSchema()
37+
38+
j, _ := json.MarshalIndent(schema, "", " ")
39+
fmt.Println(string(j))
40+
fmt.Println()
41+
}
42+
2743
func inferMultipleStringRows() {
2844
rows := []string{
2945
`{"name":"Joe", "age": 52, "something_optional": true, "something_nullable": 1.1}`,
@@ -38,6 +54,20 @@ func inferMultipleStringRows() {
3854
fmt.Println()
3955
}
4056

57+
func inferManualUnmarshal() {
58+
var m map[string]any
59+
json.Unmarshal([]byte(`{"name": "Jon", "age": 52}`), &m)
60+
61+
schema := jtdinfer.
62+
NewInferrer(jtdinfer.WithoutHints()).
63+
Infer(m).
64+
IntoSchema()
65+
66+
j, _ := json.MarshalIndent(schema, "", " ")
67+
fmt.Println(string(j))
68+
fmt.Println()
69+
}
70+
4171
func inferWithHints() {
4272
rows := []string{
4373
`{

0 commit comments

Comments
 (0)