Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Rui Gonçalves
    @ruippeixotog
    hello world
    Joao Azevedo
    @jcazevedo
    o/
    Rostyslav Khoptiy
    @Ross65536

    hello,
    Is it possible to read a YAML field as a raw string?

    I have the following example file:

    name: "John"
    obj: { "a": 2}

    And I want to obtain the case class Config(name: String, obj: String). The goal is to have a schemaless obj field

    Joao Azevedo
    @jcazevedo
    @Ross65536: Hi! You can create a YamlReader that converts the YamlValue in obj to String. Something along the lines of:
    case class Config(name: String, obj: String)
    
    implicit val configReader = new YamlReader[Config] {
      def read(yaml: YamlValue): Config = {
        yaml.asYamlObject.getFields(YamlString("name"), YamlString("obj")) match {
          case Seq(name, obj) => Config(name, obj.prettyPrint)
          case _ => deserializationError("Expected fields: 'name' and 'obj'")
        }
      }
    }
    
    val yamlStr =
      """
        |name: "John"
        |obj: { "a": 2 }
      """.stripMargin
    
    yamlStr.parseYaml.convertTo[Config]
    However, if the purpose is only to have a schema less obj field, you can read into a YamlValue instead:
    case class Config(name: String, obj: YamlValue)
    
    implicit val configReader = DefaultYamlProtocol.yamlFormat2(Config.apply)
    
    val yamlStr =
      """
        |name: "John"
        |obj: { "a": 2 }
      """.stripMargin
    
    yamlStr.parseYaml.convertTo[Config]
    Rajlakshmi
    @PatilRajlakshmi_twitter

    Hello,

    Is it possible to convert util.Map[String, Any] to Yaml object and can you please let me know how can I do it .