-
-
Notifications
You must be signed in to change notification settings - Fork 134
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
base: main
Are you sure you want to change the base?
Java enum format #173
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why separate? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean ? "EnumFormat" should { There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
} | ||
|
There was a problem hiding this comment.
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)There was a problem hiding this comment.
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 :)