purescript-protobuf icon indicating copy to clipboard operation
purescript-protobuf copied to clipboard

Support JSON

Open jamesdbrock opened this issue 9 months ago • 2 comments

https://protobuf.dev/programming-guides/proto3/#json

To add JSON support, I think we would want to generate two more functions for each message named MyMessage.

  1. putJsonMyMessage :: MyMessage -> String
  2. parseJsonMyMessage :: parseJsonMyMessage :: forall m. Monad m => ParserT String m MyMessage

I'm really not sure about those types, but that's approximately what we would want. Probably we would want to depend on argonaut-core or json and use their types.

To be able to generate those two functions, we would need to:

  1. Write a JSON version of Encode
  2. Write a JSON version of Decode
  3. Write a JSON version of genFieldPut named genFieldPutJson and add it to the generated code here.
  4. Write a JSON version of genFieldParser named genFieldParseJson and add it to the generated code here.
  5. Write a JSON version of Runtime
  6. Write a putJson message-name code generator here.
  7. Write a parseJson message-name code generator here.

jamesdbrock avatar Mar 03 '25 00:03 jamesdbrock

To turn on conformance tests for JSON:

This reply function needs a case for proto3 JSON:

https://github.com/rowtype-yoga/purescript-protobuf/blob/48ba4bcdfba9a113ccd9ed152bf4d84e25a98ffe/conformance/src/Main.purs#L67

So add a pattern matching case like:

reply (ConformanceRequest
  { requested_output_format: Just C.WireFormat_JSON -- for JSON
  , message_type: _
  , test_category: Just C.TestCategory_JSON_Test -- for JSON

jamesdbrock avatar Mar 03 '25 15:03 jamesdbrock

This is some spec:

https://protobuf.dev/programming-guides/json/

i-am-the-slime avatar Mar 03 '25 17:03 i-am-the-slime