Fork me on GitHub
#beginners
<
2021-11-15
>
Sathiya17:11:42

Hi, I am trying to save a map of values as string. I'm able to convert and save but i could reverse the same while. read-string throws exception when it reads date values. Is there a better way to transform only vectors, sets and maps and leave string values as such. I would like this

{"name" "john doe",
 "age" "28",
 "hobbies" "#{\"reading\" \"gaming\"}",
 "dob" "2021-01-06 10:58",
 "fav" "[\"foo\" \"bar\" \"baz\"]"}
to be transformed back to
{:name "john doe", :age 28, :hobbies #{"reading" "gaming"}, :dob "2021-01-06 10:58", :fav ["foo" "bar" "baz"]}
Thanks in advance

hiredman17:11:15

you are doing something weird when saving it

hiredman17:11:31

like, just call pr-str

hiredman17:11:58

that almost looks like it was serialized twice

hiredman17:11:25

the way the values are all serialized as strings

hiredman17:11:03

user=> (pr-str {:name "john doe", :age 28, :hobbies #{"reading" "gaming"}, :dob "2021-01-06 10:58", :fav ["foo" "bar" "baz"]})
"{:name \"john doe\", :age 28, :hobbies #{\"reading\" \"gaming\"}, :dob \"2021-01-06 10:58\", :fav [\"foo\" \"bar\" \"baz\"]}"
user=> (read-string (pr-str {:name "john doe", :age 28, :hobbies #{"reading" "gaming"}, :dob "2021-01-06 10:58", :fav ["foo" "bar" "baz"]}))
{:name "john doe", :age 28, :hobbies #{"reading" "gaming"}, :dob "2021-01-06 10:58", :fav ["foo" "bar" "baz"]}
user=>

Brandon Olivier17:11:39

I think they’re trying to serialize only the values

Brandon Olivier17:11:56

which, you could still use pr-str for that.

Sathiya18:11:29

pr-str actually doesnt work for me. i need the values to be saved as string to do searches on it. pr-str wraps the string in another string causing searches to fail

hiredman18:11:47

whatever you are doing does

hiredman18:11:48

like, you are doing some kind of custom serialization, and expecting to be able to call read-string and have it work

Sathiya18:11:17

yeah i used str on all the values in the map

hiredman18:11:09

read-string reads stuff that is printed as if by pr

hiredman18:11:35

if you are not using pr, then read-string isn't going to work

Sathiya18:11:42

im trying to use this to save in redis. when i save with pr-str the value gets saved as "\"john doe\"" instead of "john doe" . This is a problem. Im trying to find a way around it

hiredman18:11:43

generally, I dunno with redis, but like with say lucene, you would save extracted text and serialized datastrucutres separately

Sathiya18:11:58

that was my last resort. i was hoping if there would be simpler library or way to achieve this.

hiredman18:11:02

generic search stuff, in order to work is going to want an undifferentiad slug of flat text

hiredman18:11:43

but a format like edn in clojure or json requires markers in the text to correctly deserialize

tschady23:11:49

how do I import a java project using deps.edn? is :deps only for clojars artifacts? First time trying java interop with a non-mainline library. There’s an artifact in Maven.

seancorfield23:11:23

:deps {the.java/project {:mvn/version "1.2.3"}} will work for a project on Maven (substitute the real coordinates of the library).

seancorfield23:11:49

@tws are you getting an error?

hiredman23:11:05

the verb "import" is usually used to describe making java types available via their short name (without the package part), that is what it is called in java (the import statement) and clojure has an import macro and an :import clause for ns forms that does the same

hiredman23:11:02

which is a distinct thing from adding a dependency to your project (which you would do via deps.edn or project.clj, or pom.xml, or whatever)

tschady23:11:33

I’m trying to pull in https://repo1.maven.org/maven2/dnsjava/dnsjava/3.4.2/ with dnsjava/dnsjava {:mvn/version "3.4.2"} and i see the jar in ~/.m2/repository/dnsjava/dnsjava/3.4.2 but I don’t seem able to use it correctly. Yes, @seancorfield the error says “PEBKAC”? 😆

hiredman23:11:40

the error will be something like "class not found" or "cannot resolve whatever"

tschady23:11:36

i’ll keep at it, i’m on the right track, thanks. i am super biased against java interop syntax, and it’s holding me back

hiredman23:11:10

either because you are trying to use the short name without importing it, or the class you are trying to use doesn't exist

hiredman23:11:56

the class won't exist either because the dependency isn't correctly added, or because you are looking at examples/docs that don't correspond with the version you are using

tschady23:11:11

CIDER can’t find it with M-x browse-ns-all either after jacking in

hiredman23:11:37

java doesn't have namespaces

hiredman23:11:10

I don't use cider so I don't know what tools it has, but if that just browses clojure namespaces, then java stuff would not be in it

hiredman23:11:40

depending on how it defines all namespaces, clojure stuff might not be in it either if it hasn't been loaded

tschady23:11:00

ok, think I got it working. this is what happens when I flail without understanding anything.

seancorfield23:11:53

(! 505)-> clj -Sdeps '{:deps {dnsjava/dnsjava   {:mvn/version "3.4.2"}}}'
Downloading: dnsjava/dnsjava/3.4.2/dnsjava-3.4.2.pom from central
Downloading: dnsjava/dnsjava/3.4.2/dnsjava-3.4.2.jar from central
Clojure 1.10.3
user=> (import org.xbill.DNS.SimpleResolver)
org.xbill.DNS.SimpleResolver
user=> (.getAddress (SimpleResolver. ""))
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See  for further details.
#object[java.net.InetSocketAddress 0x659a2455 ""]
user=> 
@tws Does that help ☝️:skin-tone-2: ?

tschady23:11:22

haha, ya, that’s what I just got too.

1
hiredman23:11:31

the java standard library is perfectly capable of resolving names to addresses without an additional dependency if that is what you want

seancorfield23:11:36

API docs for that library are here, if that helps: https://javadoc.io/doc/dnsjava/dnsjava/latest/index.html

hiredman23:11:09

there are some provisos there, if I recall by default the jvm's built in resolving caches for a long time

tschady23:11:17

thanks @hiredman but I’m doing a lot of dig stuff. Yep @seancorfield that’s where I found it. I was just being a dummy on syntax.

tschady23:11:19

there are a couple ancient clojure wrappers for dnsjava, but they’re all about 8+ yrs old and don’t support much of the API.

tschady23:11:47

y’all are great

tschady23:11:51

seems like it uses some system properties. any best practices besides (System/setProperty… )?