extra.SetNamingStrategy(extra.LowerCaseWithUnderscores)
err := jsoniter.NewEncoder(into).Encode(&from)
if err != nil {
return errors.Wrap(err, errEncoding)
}
return nil
@named
annotation (usually for exceptional cases)
@taowen I submitted a PR to add the ability to specify a custom map key sorting function, to allow for, for example, certain map keys to always appear at the top of the serialized map string. Builds are passing, here is the PR and the issue. Thanks!
with this as benchmark code
func BenchmarkMarshallStd(b *testing.B) {
j := CalendarOutput{}
_ = json.Unmarshal([]byte(outputSmallJsonString), &j)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = json.Marshal(j)
}
}
func BenchmarkMarshallIter(b *testing.B) {
j := CalendarOutput{}
_ = json.Unmarshal([]byte(outputSmallJsonString), &j)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(j)
}
}
I get these results
Mathieus-MacBook-Pro:cmd mhindery$ go test -bench=^BenchmarkMarshall.* -benchmem -cpu 1
goos: darwin
goarch: amd64
pkg: goapi/cmd
BenchmarkMarshallStd 1000000 2408 ns/op 332 B/op 7 allocs/op
BenchmarkMarshallIter 1000000 2669 ns/op 376 B/op 8 allocs/op
PASS
v1.1.8
release? it was mistakenly tagged as 1.1.8
(without the v
).go get github.com/json-iterator/go@v1.1.8
fails
@atararaksin Hi, Andrey! If you are asking about Scala reincarnation of jsoniter and your case mentioned in the circe chat then try the following example:
import com.github.plokhotnyuk.jsoniter_scala.macros._
import com.github.plokhotnyuk.jsoniter_scala.core._
import java.io.ByteArrayInputStream
case class Foo(x: Float, y: Float)
val fooCodec: JsonValueCodec[Foo] = JsonCodecMaker.make(CodecMakerConfig)
def customArrayCodec(name: String)(handler: Foo => Unit): JsonValueCodec[Unit] = new JsonValueCodec[Unit] {
override def decodeValue(in: JsonReader, default: Unit): Unit = {
if (in.isNextToken('{')) {
in.skipToKey("foos")
if (in.isNextToken('[')) {
do handler(fooCodec.decodeValue(in, fooCodec.nullValue))
while (in.isNextToken(','))
}
if (!in.isNextToken('}')) in.skipToKey("")
}
}
override def encodeValue(x: Unit, out: JsonWriter): Unit = ()
override val nullValue: Unit = ()
}
val in = new ByteArrayInputStream("""{"name":"test","foos":[{"x":1.0,"y":2.0},{"x":3.0,"y":4.0}],"bars":[]}""".getBytes("UTF-8"))
readFromStream[Unit](in)(customArrayCodec("test") { foo =>
println(foo)
})
It should print:
Foo(1.0,2.0)
Foo(3.0,4.0)