0
votes
class Converter extends 
RootJsonFormat[TypeToConvert]
{
def write(obj : TypeToConvert) : JsValue =
{
    obj match
    {
        case Https => JsString(Https.name)
        case Sms => JsString(Sms.name)
    }
}   
def read(jsValue : JsValue) : TypeToConvert =
{
    jsValue match
    {
        case JsString("HTTPS") => Https
        case JsString("SMS") => Sms
        case _ => throw 
  DeserializationException("Expected a TypeToConvert
    Type")
     }
  }
}

I have written a converter like this and in this I am using only write method. Is there a way to write json formatters with only write method. I want to remove read method.

1
i don't think it is possible to do so you will have to write both read, as well as write methods. - Raman Mishra

1 Answers

0
votes
trait RootJsonFormat[T] extends JsonFormat[T] with RootJsonReader[T] with RootJsonWriter[T]

As you can see this is how the hirearchy is of RootJsonFormat which extends JsonFormat. Which is using RootJsonReader as well as RootJsonWriter. If you want to use only the JsonWriter. Then extend your class with RootJsonWriter.

You are using RootJsonFormat with your class in that case you will have to use define write and read methods.

class Converter extends 
RootJsonWriter[TypeToConvert]
{
def write(obj : TypeToConvert) : JsValue =
{
    obj match
    {
        case Https => JsString(Https.name)
        case Sms => JsString(Sms.name)
    }
}   
}