generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from ipfs/from-gdr
- Loading branch information
Showing
11 changed files
with
452 additions
and
348 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package drjson | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
) | ||
|
||
func marshalJSON(val any) (*bytes.Buffer, error) { | ||
buf := &bytes.Buffer{} | ||
enc := json.NewEncoder(buf) | ||
enc.SetEscapeHTML(false) | ||
err := enc.Encode(val) | ||
return buf, err | ||
} | ||
|
||
// MarshalJSONBytes is needed to avoid changes | ||
// on the original bytes due to HTML escapes. | ||
func MarshalJSONBytes(val any) ([]byte, error) { | ||
buf, err := marshalJSON(val) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// remove last \n added by Encode | ||
return buf.Bytes()[:buf.Len()-1], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package drjson | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMarshalJSON(t *testing.T) { | ||
// ensure that < is not escaped, which is the default Go behavior | ||
bytes, err := MarshalJSONBytes(map[string]string{"<": "<"}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
require.Equal(t, "{\"<\":\"<\"}", string(bytes)) | ||
} |
Oops, something went wrong.