0

I would like to serialize small drawings in Rasterific to JSON.

I want to serialize programs/drawing actions such as:

withTexture (uniformTexture drawColor) $ do
            fill $ circle (V2 0 0) 30
            stroke 4 JoinRound (CapRound, CapRound) $
                   circle (V2 400 200) 40

How can I do such an serialization to JSON?

My ideas so far: Perhaps it is better to use the monoid instance. There is also an answer on stackoverflow that advises to use a 'data type' for function calls and an interpreter function that evaluated the code (ie. calls the function): How to serialize function type to json in haskell?

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
mrsteve
  • 4,082
  • 1
  • 26
  • 63
  • Monads are allowed to do arbitrary computation (within the limits of their effects). The only serialisation of that that I can think of is the original Haskell source code (not available at runtime). In Haskell functions are (first class) values but not data. Applicatives would also not be serialisable for the same reason. You want some representation of your drawings with only data (no functions or applicatives), e.g. a list of drawing commands (begin path, move to, stroke path, push transform,...). Or perhaps you could output your drawings to svg or some other standard drawing description. – Dan Robertson Jan 20 '18 at 00:08
  • Alternatively you could do extensive dsl work to serialise some things (eg like in `sbv`) but you definitely can’t deserialise such things into programs with performance comparable to compiled programs (likely not even comparable to interpreted languages) – Dan Robertson Jan 20 '18 at 00:11
  • I don't need that much performance. I also can use just a 'Monoid'. Will ***Monoid*** work better? – mrsteve Jan 21 '18 at 10:09
  • Without knowing any details, `Monoid` seems like a better starting point – Dan Robertson Jan 21 '18 at 12:39

0 Answers0