Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Array support #136

Closed
kazegusuri opened this issue Aug 20, 2016 · 5 comments
Closed

JSON Array support #136

kazegusuri opened this issue Aug 20, 2016 · 5 comments
Assignees

Comments

@kazegusuri
Copy link

zap supports nested JSON Objects with zap.Nest or zap.Marshaler, but does not support JSON Array yet. Current workaround is to use zap.Object and implement MarshalJSON to output JSON Array. How about built-in support for JSON Array like zap.Array?

@akshayjshah
Copy link
Contributor

akshayjshah commented Aug 20, 2016

That's definitely on our radar. For now, you can use zap.Object - as long as you're passing a slice of primitive types, you don't even have to implement MarshalJSON.

The best way I can think of to support JSON arrays (while keeping performance good) is to add specific helpers for each slice type. We'd end up with zap.Ints, zap.Floats, zap.Durations, etc.

Are you interested in working on this feature?

@jcorbin
Copy link
Contributor

jcorbin commented Nov 16, 2016

@akshayjshah , what do you think of the following approach:

diff --git a/keyvalue.go b/keyvalue.go
index cda24f5..862ebb3 100644
--- a/keyvalue.go
+++ b/keyvalue.go
@@ -38,4 +38,26 @@ type KeyValue interface {
    // allocation-heavy. Consider implementing the LogMarshaler interface instead.
    AddObject(key string, value interface{}) error
    AddString(key, value string)
+   AddArray(key, valueAdder func(Values) error)
+}
+
+// Values is an encoding agnostic interface to add array data to a keyed value
+// within a logging context. Like slices, Values aren't safe for concurrent use
+// (though typical use shouldn't require locks).
+//
+// XXX example
+type Values interface {
+   AddBool(value bool)
+   AddFloat64(value float64)
+   AddInt(value int)
+   AddInt64(value int64)
+   AddUint(value uint)
+   AddUint64(value uint64)
+   AddUintptr(value uintptr)
+   AddMarshaler(marshaler LogMarshaler) error
+   // AddObject uses reflection to serialize arbitrary objects, so it's slow and
+   // allocation-heavy. Consider implementing the LogMarshaler interface instead.
+   AddObject(value interface{}) error
+   AddString(value string)
+   AddArray(valueAdder func(Values) error) // XXX to taste depending on how recursive you're feeling this morning ;-)
 }

@akshayjshah akshayjshah added this to the 1.0.0 milestone Jan 3, 2017
@akshayjshah
Copy link
Contributor

This is in-progress on the dev branch: #211

@akshayjshah
Copy link
Contributor

Further support in #258.

akshayjshah pushed a commit that referenced this issue Feb 1, 2017
Add wrappers for common array types. This fixes #136.
akshayjshah added a commit that referenced this issue Feb 1, 2017
Add wrappers for common array types. This fixes #136.
akshayjshah added a commit that referenced this issue Feb 1, 2017
Add wrappers for common array types. This fixes #136.
akshayjshah added a commit that referenced this issue Feb 1, 2017
Add wrappers for common array types. This fixes #136.
@akshayjshah
Copy link
Contributor

Landed on dev.

akshayjshah added a commit that referenced this issue Feb 15, 2017
Add wrappers for common array types. This fixes #136.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants