GithubHelp home page GithubHelp logo

Transparent (de)serialization about geom HOT 3 CLOSED

heyarne avatar heyarne commented on June 9, 2024
Transparent (de)serialization

from geom.

Comments (3)

dimovich avatar dimovich commented on June 9, 2024 3

Hello @postspectacular, @heyarne.

As per the official guide (https://clojure.org/reference/reader#tagged_literals)
"Reader tags without namespace qualifiers are reserved for Clojure."

So maybe #vec2 can be changed to #thi.ng/vec2?

It seems print-method definitions are only needed for the Clojure part of vectors.
For ClojureScript, vectors already implement IPrintWithWriter
https://github.com/thi-ng/geom/blob/feature/no-org/src/thi/ng/geom/vector.cljc#L361-L364

The code you tried for the data readers:
{'thi.ng.geom.types.Circle2 c/circle}

will only work in Clojure because the reader will automatically initialize the record without calling the function you specified. But if you try to run this in ClojureScript, c/circle will be called with a map, and will return:
#thi.ng.geom.types.Circle2{:p [0 0], :r {:p [0 0], :r 100}}.

The proper way would be to use the automatically generated map->Circle2 function.

'thi.ng.geom.types.Circle2 thi.ng.geom.types/map->Circle2

I've made the proposed changes in my local repository. It needs more testing but so far it seems to work and I can send a PR -- dimovich@e0aa76a

from geom.

postspectacular avatar postspectacular commented on June 9, 2024

Hey @heyarne! I don't remember the specifics, but serialization related to pr-writer & print-method stuff used to be semi-magic, undocumented and had some weird quirks & differences between CLJ & CLJS. It seems things have changed/improved since 2014 😄 ... We could try adding the following multimethod impls and test/experiment more with those:

(require '[thi.ng.geom.vector :as v])

(defmethod print-method thi.ng.geom.vector.Vec2 [a ^java.io.Writer w]
  (.write w (print-str "#vec2" (.toString a))))

(defmethod print-method thi.ng.geom.vector.Vec3 [a ^java.io.Writer w]
  (.write w (print-str "#vec3" (.toString a))))

(v/vec2 10 20)
; #vec2 [10.0 20.0]

Testing this with *data-readers* seems to work now:

(require
  '[thi.ng.geom.core :as g]
  '[thi.ng.geom.circle :as c])

(def readers {
  'thi.ng.geom.types.Circle2 c/circle
  'vec2 v/vec2
})

(def foo (binding [*data-readers* readers]
  (read-string (pr-str (c/circle 100)))))
; #'user/foo

foo
; #thi.ng.geom.types.Circle2{:p #vec2 [0.0 0.0], :r 100.0}

(g/as-polygon foo 3)
; #thi.ng.geom.types.Polygon2{:points [#vec2 [100.0 0.0] #vec2 [-49.99999999999998 86.60254037844388] #vec2 [-50.00000000000004 -86.60254037844383]]}

I haven't got bandwidth to test this further right now, but if you do and find it's working, then I'd be grateful for a PR. Also related to this (and nice for UX/DX) would be adding a data_readers.cljc file...

from geom.

postspectacular avatar postspectacular commented on June 9, 2024

Thanks, @dimovich - sure my Clojure foo is slowly getting rusty! I had a hunch about map->Circle2 probably being the right choice, but only quickly tested this in the Clojure REPL and it seemed to work... These changes look good & thank you for offering a PR, looking forward to it!

from geom.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.