clj-yaml

RAJKUMAR 2024-08-16T20:22:31.763599Z

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

borkdude 2024-08-16T20:22:44.985919Z

Please use a thread

👍 1
RAJKUMAR 2024-08-16T20:23:21.602149Z

I need to convert that into yaml format

borkdude 2024-08-16T20:23:37.885249Z

how would you represent a regex in yaml?

RAJKUMAR 2024-08-16T20:23:55.913649Z

Here is the sample code

RAJKUMAR 2024-08-16T20:23:58.005809Z

(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})

RAJKUMAR 2024-08-16T20:24:26.302049Z

I get this error

RAJKUMAR 2024-08-16T20:24:28.391759Z

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

borkdude 2024-08-16T20:24:40.042529Z

You didn't answer my question

borkdude 2024-08-16T20:24:50.800189Z

(yet) ;)

RAJKUMAR 2024-08-16T20:25:31.802049Z

The result I would like to get is

RAJKUMAR 2024-08-16T20:25:34.551389Z

"a: a*b\n"

borkdude 2024-08-16T20:26:41.358049Z

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

RAJKUMAR 2024-08-16T20:28:48.836959Z

sounds good

borkdude 2024-08-16T20:29:50.716859Z

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

RAJKUMAR 2024-08-16T23:25:02.848029Z

Thanks @borkdude

RAJKUMAR 2024-08-16T23:25:13.345979Z

this is what I was looking for !!!