# Any elm CLI JSON codegen libraries, like flutter's JsonSerializable/build runner?

**URL:** <https://discourse.elm-lang.org/t/any-elm-cli-json-codegen-libraries-like-flutters-jsonserializable-build-runner/7771>\
**Category:** Learn\
**Created:** [September 20, 2021, 5:30pm UTC](https://discourse.elm-lang.org/t/any-elm-cli-json-codegen-libraries-like-flutters-jsonserializable-build-runner/7771 "2021-09-20T17:30:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Valian\_Masdani](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/valian_masdani/32/3734_2.png) [@Valian\_Masdani](https://discourse.elm-lang.org/u/Valian_Masdani)\
**Post date:** [September 20, 2021, 5:30pm UTC](https://discourse.elm-lang.org/t/any-elm-cli-json-codegen-libraries-like-flutters-jsonserializable-build-runner/7771/1 "2021-09-20T17:30:15Z")

</div>

Hello. I basically want to mimic this feature that I found on Flutter, which maps dart objects like this:

```auto
@JsonSerializable()
class Person {
  Person();

  int? id;
  String? name;

  @JsonKey(name: 'date_of_birth')
  int? dateOfBirth;

  factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
  Map<String, dynamic> toJson() => _$PersonToJson(this);
}

```

and then when we run in the cli:

```auto
flutter pub run build_runner build

```

the script will create an additional `model.g.dart` file which contains the de/serialization code like this:

```auto
Person _$PersonFromJson(Map<String, dynamic> json) => Person()
  ..id = json['id'] as int?
  ..name = json['name'] as String?
  ..dateOfBirth = json['date_of_birth'] as int?;

Map<String, dynamic> _$PersonToJson(Person instance) => <String, dynamic>{
      'id': instance.id,
      'name': instance.name,
      'date_of_birth': instance.dateOfBirth,
    };

```

Now I want this for Elm. For example if we have an elm class like this

```elm
type alias Customer =
    { -- Base
      uuid : Maybe String
    , createdAt : Maybe Int
    , updatedAt : Maybe Int
    , deletedAt : Maybe Int

    -- Base end
    , name : Maybe String
    , phone : Maybe String
    , address : Maybe String
    }

```

and then we run something like this:

```auto
npm run elm-record-codegen --case camelCase --type pipeline

```

we get a file containing the automatically mapped en/decoders for the class

```elm
customerEncoder : Customer -> Encode.Value
customerEncoder customer =
    Encode.object
        [ -- Base
          ( "uuid", EncodeExtra.maybe Encode.string customer.uuid )
        , ( "createdAt", EncodeExtra.maybe Encode.int customer.createdAt )
        , ( "updatedAt", EncodeExtra.maybe Encode.int customer.updatedAt )
        , ( "deletedAt", EncodeExtra.maybe Encode.int customer.deletedAt )

        -- Base end
        , ( "name", EncodeExtra.maybe Encode.string customer.name )
        , ( "phone", EncodeExtra.maybe Encode.string customer.phone )
        , ( "address", EncodeExtra.maybe Encode.string customer.address )
        ]

customerDecoder : Decode.Decoder Customer
customerDecoder =
    Decode.succeed Customer
        -- Base
        |> Pipeline.required "uuid" (Decode.maybe Decode.string)
        |> Pipeline.required "createdAt" (Decode.maybe Decode.int)
        |> Pipeline.required "updatedAt" (Decode.maybe Decode.int)
        |> Pipeline.required "deletedAt" (Decode.maybe Decode.int)
        -- Base end
        |> Pipeline.required "name" (Decode.maybe Decode.string)
        |> Pipeline.required "phone" (Decode.maybe Decode.string)
        |> Pipeline.required "address" (Decode.maybe Decode.string)

```

Is there any package which behaves like this? If not, what should I learn to create something like this? I imagine I need an elm source code parser similar to [rust’s token parser](https://github.com/dtolnay/syn), detect `type alias`es in source code, and then finally generate json en/decoders.

It will tremendously help me in creating elm apps which use lots of data with lots of complex data structures, so that i don’t have to type too much boilerplate en/decoders.

---

<div class="post-metadata">

**Author:** ![MartinS](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/martins/32/3137_2.png) [@MartinS](https://discourse.elm-lang.org/u/MartinS)\
**Post date:** [September 20, 2021, 6:10pm UTC](https://discourse.elm-lang.org/t/any-elm-cli-json-codegen-libraries-like-flutters-jsonserializable-build-runner/7771/2 "2021-09-20T18:10:08Z")

</div>

I have an [unreleased package](https://github.com/MartinSStewart/elm-review-todo-it-for-me) that sort of does what you have in mind.

It lets you write

```auto
type alias Customer =
    { -- Base
      uuid : Maybe String
    , createdAt : Maybe Int
    , updatedAt : Maybe Int
    , deletedAt : Maybe Int

    -- Base end
    , name : Maybe String
    , phone : Maybe String
    , address : Maybe String
    }

customerCodec : Codec e Customer
customerCodec = Debug.todo ""

```

and then run the package via [elm-review](https://package.elm-lang.org/packages/jfmengels/elm-review/latest/) to generate the code (you should be able to run it without any set up if you have Node installed and run this `npx elm-review --template MartinSStewart/elm-review-todo-it-for-me/preview --fix-all`).

Currently it only supports generating codecs for my elm-serialize package. There’s no support for generating ordinary `elm/json` encoders and decoders but pull requests are welcome.

One difference between this approach and what you might have in mind is that this is “best effort”. There are situations where you’ll have to fill in the blanks. But the hope is to remove most of the burden from writing serialization.

---

<div class="post-metadata">

**Author:** ![gorgoroth](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/gorgoroth/32/3355_2.png) [@gorgoroth](https://discourse.elm-lang.org/u/gorgoroth)\
**Post date:** [September 21, 2021, 6:23am UTC](https://discourse.elm-lang.org/t/any-elm-cli-json-codegen-libraries-like-flutters-jsonserializable-build-runner/7771/3 "2021-09-21T06:23:11Z")

</div>

Maybe this is not exactly what you want, but

[https://korban.net/elm/json2elm/](https://korban.net/elm/json2elm/)

it is a wonderful starting point

---

<div class="post-metadata">

**Author:** ![pravdomil](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/pravdomil/32/4182_2.png) [@pravdomil](https://discourse.elm-lang.org/u/pravdomil)\
**Post date:** [September 24, 2021, 11:23am UTC](https://discourse.elm-lang.org/t/any-elm-cli-json-codegen-libraries-like-flutters-jsonserializable-build-runner/7771/4 "2021-09-24T11:23:37Z")

</div>

Hi, I’m using:

> **[GitHub - pravdomil/Elm-JSON-Interop: Generate JSON encoders/decoders based on...](https://github.com/pravdomil/Elm-JSON-Interop)**
>
> Generate JSON encoders/decoders based on Elm types. - GitHub - pravdomil/Elm-JSON-Interop: Generate JSON encoders/decoders based on Elm types.

It generates JSON encoders/decoders based on Elm types.

---

<div class="post-metadata">

**Author:** ![FranzSkuffka](https://avatars.discourse-cdn.com/v4/letter/f/c4cdca/32.png) [@FranzSkuffka](https://discourse.elm-lang.org/u/FranzSkuffka)\
**Post date:** [September 25, 2021, 3:06pm UTC](https://discourse.elm-lang.org/t/any-elm-cli-json-codegen-libraries-like-flutters-jsonserializable-build-runner/7771/5 "2021-09-25T15:06:36Z")

</div>

My homebrew:

> **[GitHub - janwirth/elm-coder-generator: Generate encoders and decoders for elm/json](https://github.com/janwirth/elm-coder-generator)**
>
> Generate encoders and decoders for elm/json

> [@Elm coder generator - RELOADED](https://discourse.elm-lang.org/t/elm-coder-generator-reloaded/4474):
>
> More than a year ago [Dániel Kodaj presented his decoder generator package](https://discourse.elm-lang.org/t/decoder-generator-and-imports-cleanup-tool/1399/2). I heart it. It is such an awesome tool. Over the recent months I have done some work on it. I made it compatible with elm 0.19, fixed some bugs and created a CLI. Check it out here: I hope this really saves you as much time as it saves me. Big ups to Dániel for this! Feedback is greatly appreciated. What do you think of the API and the code annoation format? I would like to provide this to more users VSCode for…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [October 5, 2021, 3:07pm UTC](https://discourse.elm-lang.org/t/any-elm-cli-json-codegen-libraries-like-flutters-jsonserializable-build-runner/7771/6 "2021-10-05T15:07:08Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
