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

Java enum format #173

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2009-2018 Lightbend Inc. <https://www.lightbend.com>
*/

package play.api.libs.json

/**
* Helper for format a java enum into or from json
*
* public enum TestEnum {
*
* TEST1,
* TEST2,
* OKAY
*
* }
*
* implicit val TestEnumFormat = JavaEnumFormat.format[TestEnum]
*
*/
object JavaEnumFormat {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With not with other default? (Take care about @author vs CLA)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about this will remove it :)

def format[A <: Enum[A]](implicit m: scala.reflect.Manifest[A]): Format[A] = {
new JsonJavaEnumFormat[A]
}
}

/**
* Format for java enums from and to json
*
* @tparam A the type of the enum
*/
class JsonJavaEnumFormat[A <: Enum[A]](implicit m: scala.reflect.Manifest[A]) extends Format[A] {
def reads(json: JsValue) = JsSuccess(Enum.valueOf[A](m.runtimeClass.asInstanceOf[Class[A]], json.as[String]))

def writes(enumValue: A) = JsString(enumValue.name())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (C) 2009-2018 Lightbend Inc. <https://www.lightbend.com>
*/

package play.api.libs.json;


public enum JavaTestEnum {

TEST_1(true),

TEST_2(false);

public final boolean testPriv;

JavaTestEnum(boolean testPriv) {
this.testPriv = testPriv;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright (C) 2009-2018 Lightbend Inc. <https://www.lightbend.com>
*/

package play.api.libs.json

object JavaTestsEnumsFormat {
Copy link
Member

@cchantep cchantep Sep 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why separate?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean ?
I looked how the other enum tests are working

"EnumFormat" should {
import TestEnums.EnumWithCustomNames._

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to have a separate file, whereas there are existings file for format tests

implicit val testsEnumsFormat = JavaEnumFormat.format[JavaTestEnum]
}

Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ class ReadsSharedSpec extends WordSpec with MustMatchers {
}
}

"JavaEnumFormat" should {
import JavaTestsEnumsFormat._
"deserialize correctly java enum with names" in {
JsString("TEST_1").validate[JavaTestEnum] mustEqual JsSuccess(JavaTestEnum.TEST_1)
JsString("TEST_2").validate[JavaTestEnum] mustEqual JsSuccess(JavaTestEnum.TEST_2)
}
}

// ---

case class Owner(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ class WritesSharedSpec extends WordSpec with MustMatchers {
}
}

"JavaEnumFormat" should {
import JavaTestsEnumsFormat._
"serialize correctly java enum with names" in {
Json.toJson(JavaTestEnum.TEST_1) mustEqual JsString("TEST_1")
Json.toJson(JavaTestEnum.TEST_2) mustEqual JsString("TEST_2")
}
}

// ---

case class Location(lat: Double, long: Double)
Expand Down