diff --git a/genson-scala/src/main/scala/com/owlike/genson/ext/json4s/Json4SBundle.scala b/genson-scala/src/main/scala/com/owlike/genson/ext/json4s/Json4SBundle.scala index dfaebe0a..457e51c9 100644 --- a/genson-scala/src/main/scala/com/owlike/genson/ext/json4s/Json4SBundle.scala +++ b/genson-scala/src/main/scala/com/owlike/genson/ext/json4s/Json4SBundle.scala @@ -13,7 +13,7 @@ import org.json4s.JsonAST.JDecimal import org.json4s.JsonAST.JArray import scala.collection.mutable.ArrayBuffer -object Json4SBundle extends GensonBundle { +class Json4SBundle extends GensonBundle { def configure(builder: GensonBuilder) { builder.withConverterFactory(new Factory[Converter[JValue]]() { @@ -22,6 +22,10 @@ object Json4SBundle extends GensonBundle { } } +object Json4SBundle { + def apply() = new Json4SBundle() +} + object JValueConverter extends Converter[JValue] { def serialize(value: JValue, writer: ObjectWriter, ctx: Context) = { diff --git a/genson-scala/src/test/scala/com/owlike/genson/ext/json4s/Json4SRoundTripTest.scala b/genson-scala/src/test/scala/com/owlike/genson/ext/json4s/Json4SRoundTripTest.scala index ca1c394e..e075b3ac 100644 --- a/genson-scala/src/test/scala/com/owlike/genson/ext/json4s/Json4SRoundTripTest.scala +++ b/genson-scala/src/test/scala/com/owlike/genson/ext/json4s/Json4SRoundTripTest.scala @@ -4,12 +4,12 @@ import org.scalatest.{Matchers, FunSuite} import com.owlike.genson.{ScalaBundle, GensonBuilder, ScalaGenson} import org.json4s._ import org.json4s.JsonDSL._ -import org.json4s.JsonAST.{JValue, JNull} +import org.json4s.JsonAST._ class Json4SRoundTripTest extends FunSuite with Matchers { val genson = new ScalaGenson( new GensonBuilder() - .withBundle(ScalaBundle(), Json4SBundle) + .withBundle(ScalaBundle(), Json4SBundle()) .create() ) diff --git a/genson/src/main/java/com/owlike/genson/convert/ClassMetadataConverter.java b/genson/src/main/java/com/owlike/genson/convert/ClassMetadataConverter.java index dcdc88c2..fc1d7ced 100644 --- a/genson/src/main/java/com/owlike/genson/convert/ClassMetadataConverter.java +++ b/genson/src/main/java/com/owlike/genson/convert/ClassMetadataConverter.java @@ -66,8 +66,7 @@ public ClassMetadataConverter(Class tClass, Converter delegate) { } public void serialize(T obj, ObjectWriter writer, Context ctx) throws Exception { - // obj != null allows to not fail in case when the NullConverter is not in the chain - if (!Object.class.equals(tClass) && obj != null) { + if (obj != null) { writer.beginNextObjectMetadata() .writeMetadata("class", ctx.genson.aliasFor(obj.getClass())); } diff --git a/website/Documentation/Extensions.md b/website/Documentation/Extensions.md index 4b914438..a2215c8c 100644 --- a/website/Documentation/Extensions.md +++ b/website/Documentation/Extensions.md @@ -65,7 +65,17 @@ public class GensonCustomResolver implements ContextResolver { **Note** -By default Genson JAX-RS integration enables JAXB annotations support. +* By default Genson JAX-RS integration enables JAXB annotations support. +* Starting with Jersey 2.9, automatic discovery is disabled, in order to enable it like in previous versions you need + to a dependency to jersey-metainf-services. + +{% highlight xml %} + + org.glassfish.jersey.ext + jersey-metainf-services + your_version + +{% endhighlight %} ##JSR 353 - Java API for Json Processing## diff --git a/website/Documentation/ScalaGuide.md b/website/Documentation/ScalaGuide.md index 171fb11b..eeee7c40 100644 --- a/website/Documentation/ScalaGuide.md +++ b/website/Documentation/ScalaGuide.md @@ -168,4 +168,52 @@ toJson(...) fromJson[SomeType](json) {% endhighlight %} -For a more in depth overview of Genson features and customization have a look at the User Guide and configuration sections. \ No newline at end of file +For a more in depth overview of Genson features and customization have a look at the User Guide and configuration sections. + + +##AST support via json4s + +Instead of creating again another DOM structure like all the existing ones, Genson provides it by supporting json4s. +Json4s defines an AST for JSON and utilities to work with it. + + +{% highlight scala %} +import com.owlike.genson._ +import org.json4s._ +import org.json4s.JsonDSL._ +import org.json4s.JsonAST._ + +object CustomGenson { + val genson = new ScalaGenson( + new GensonBuilder() + .withBundle(ScalaBundle(), Json4SBundle()) + .create() + ) +} + +// then just import it in the places you want to use this instance instead of the default one +import CustomGenson.genson._ + +// and use it! +val json = fromJson[JValue]("""{"name":"foo","someDouble":28.1,"male":true,"someArray":[1,2,3],"null":null}""") + +{% endhighlight %} + + +In order to use json4s features with Genson, you need to add a dependency to it. + +###SBT + +{% highlight scala %} +libraryDependencies += "org.json4s" % "json4s-ast_${scala.version}" % "3.2.10" +{% endhighlight %} + +###Maven + +{% highlight xml %} + + org.json4s + json4s-ast_${scala.version} + ${json4s.version} + +{% endhighlight %}