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

Cannot parse as Long from String format field #349

Open
carbonshow opened this issue May 6, 2022 · 0 comments
Open

Cannot parse as Long from String format field #349

carbonshow opened this issue May 6, 2022 · 0 comments

Comments

@carbonshow
Copy link

carbonshow commented May 6, 2022

According to the protobuf issue, int64, uint64 type is serialized into string due to a Javascript precision problem. And it seems spray cannot read the string field as Long type.

for example:

  1. case class definition: case class Test(id: String, timestamp: Long)
  2. protobuf IDL definition:
    message Test {
       string id = 1;
       int64 timestamp = 2;
    }

the output string from protobuf json looks like:

{
  "id": "xyz",
  "timestamp": "1648867560"
}

When spray parse the timestamp field as Long, got the exception: "Expected Long as JsNumber, but got "1648867560" "

Suggest:
Add the String match case for LongJsonFormat:

implicit object LongJsonFormat extends JsonFormat[Long] {
    def write(x: Long) = JsNumber(x)
    def read(value: JsValue) = value match {
      case JsNumber(x) if x.isValidLong => x.longValue
      case JsString(x) => BigDecimal(x).longValue
      case x => deserializationError("Expected Long as JsNumber, but got " + x)
    }
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant