Changes
2 changed files (+42/-16)
-
-
@@ -156,26 +156,33 @@ func (i IRI) MarshalText() ([]byte, error) {} // MarshalJSON returns the JSON document represented by the current Context // This should return : // If only one element in the context and the element has no Term -> json marshaled string // If multiple elements in the context without Term -> json marshaled array of strings // If multiple elements where at least one doesn't have a Term and one has a Term -> json marshaled array // If multiple elements where all have Terms -> json marshaled object func (c Context) MarshalJSON() ([]byte, error) { v := func(t Term, i IRI) interface{} { if t.IsNil() && i.IsNil() { return nil } if t.IsNil() && !i.IsNil() { return i arr := make([]interface{}, 0) i := 0 for t, iri := range c { if t.IsNil() { if len(c) == 1 { return json.Marshal(iri) } arr = append(arr, iri) delete(c, t) i += 1 } return map[Term]IRI{t: i} } arr := make([]interface{}, len(c)) i := 0 for term, iri := range c { el := v(term, iri) if len(c) == 1 { return json.Marshal(el) if len(c) > 0 { mapIRI := make(map[Term]IRI, len(c)) for t, iri := range c { mapIRI[t] = iri } if len(arr) == 0 { return json.Marshal(mapIRI) } arr[i] = el i += 1 arr = append(arr, mapIRI) } return json.Marshal(arr) }
-
-
-
@@ -52,4 +52,23 @@ func TestContext_MarshalJSON(t *testing.T) {if !strings.Contains(string(out), asTerm) { t.Errorf("Json doesn't contain Term %s, %s", asTerm, string(out)) } testTerm := "test_term" c3 := Context{Term(testTerm): IRI(url), Term(asTerm): IRI(asUrl)} out, err = c3.MarshalJSON() if err != nil { t.Errorf("%s", err) } if !strings.Contains(string(out), url) { t.Errorf("Json doesn't contain URL %s, %s", url, string(out)) } if !strings.Contains(string(out), asUrl) { t.Errorf("Json doesn't contain URL %s, %s", asUrl, string(out)) } if !strings.Contains(string(out), asTerm) { t.Errorf("Json doesn't contain Term %s, %s", asTerm, string(out)) } if !strings.Contains(string(out), testTerm) { t.Errorf("Json doesn't contain Term %s, %s", testTerm, string(out)) } }
-