Skip to content

Commit 4745ab3

Browse files
committed
receive jsonld is []byte and string fix error
1 parent 66941d2 commit 4745ab3

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

json_ld_reader.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package jsonld_helper
22

33
import (
4+
"encoding/json"
5+
"reflect"
6+
47
"github.com/piprate/json-gold/ld"
58
)
69

@@ -29,7 +32,22 @@ type JsonLDReader interface {
2932
}
3033

3134
func ParseJsonLD(value any, options *ld.JsonLdOptions) (JsonLDReader, error) {
32-
expanded, err := ld.NewJsonLdProcessor().Expand(value, options)
35+
origin := value
36+
reflectType := reflect.TypeOf(value).String()
37+
38+
if reflectType == "[]uint8" {
39+
if err := json.Unmarshal(value.([]byte), &origin); err != nil {
40+
return nil, err
41+
}
42+
}
43+
44+
if reflectType == "string" {
45+
if err := json.Unmarshal([]byte(value.(string)), &origin); err != nil {
46+
return nil, err
47+
}
48+
}
49+
50+
expanded, err := ld.NewJsonLdProcessor().Expand(origin, options)
3351

3452
if err != nil {
3553
return nil, err

json_ld_reader_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -188,26 +188,26 @@ func Test_READMEJsonLDTest(t *testing.T) {
188188
}
189189

190190
var jsonld, _ = jsonld_helper.ParseJsonLD(givenJsonLD, nil)
191-
var givenJsonLD = map[string]any{
192-
"@context": []any{
191+
var givenJsonLD = `{
192+
"@context": [
193193
"activitystreams.json",
194-
map[string]any{
195-
"schema": "http://schema.org#",
194+
{
195+
"schema": "http://schema.org#",
196196
"PropertyValue": "schema:PropertyValue",
197-
"value": "schema:value",
198-
},
199-
},
197+
"value": "schema:value"
198+
}
199+
],
200200
"as:type": "Person",
201-
"@id": "acct:[email protected]",
202-
"name": "지상 최강의 개발자 쥬니니",
203-
"attachment": []map[string]any{
201+
"@id": "acct:[email protected]",
202+
"name": "지상 최강의 개발자 쥬니니",
203+
"attachment": [
204204
{
205-
"type": "PropertyValue",
206-
"name": "GitHub",
207-
"value": "juunini",
208-
},
209-
},
210-
}
205+
"type": "PropertyValue",
206+
"name": "GitHub",
207+
"value": "juunini"
208+
}
209+
]
210+
}`
211211
var readmeJsonLD = map[string]any{
212212
"@context": []any{
213213
"activitystreams.json",

0 commit comments

Comments
 (0)