clj-yaml 2024-08-16

I've an object that contains java.util.regex.Pattern

Please use a thread

👍 1

I need to convert that into yaml format

how would you represent a regex in yaml?

Here is the sample code

(ns yaml-conversion.demo
  (:require [clojure.string :as str]
            [clj-yaml.core :as yaml])
  (:import (java.util.regex Pattern)))

(set! *warn-on-reflection* true)

(defn jet-jvm [input & args]
  (str/replace
   (with-out-str
     (with-in-str input
       (apply jet/-main args)))
   "\r\n"
   "\n"))


(def pattern (Pattern/compile "a*b"))

(def input {:a pattern})

(yaml/generate-string input :dumper-options {:flow-style :block})

I get this error

> Execution error (YAMLException) at org.yaml.snakeyaml.introspector.PropertyUtils/getPropertiesMap (PropertyUtils.java:108) . > No JavaBean properties found in java.util.regex.Pattern

You didn't answer my question

The result I would like to get is

I think your best bet would be to just postwalk the expression and stringify the regex, before feeding it to clj-yaml

sounds good

you could also use the protocol:

(extend-protocol YAMLCodec
  java.util.regex.Pattern
  ...
See here for an example: https://github.com/clj-commons/clj-yaml/blob/861271d408377f03b3ed9de547a86b0831c40f6a/src/clojure/clj_yaml/core.clj#L205

this is what I was looking for !!!