title: What's the best approach to model with freezed's Union/Sealed Classes JSON serialiation when you can't control the structure of the provided JSON?
body: <p>Given an unmodifiable JSON you get from the server like this:</p>
  <pre class="lang-json prettyprint-override"><code>{ &quot;name&quot;: &quot;someName&quot;,
    &quot;fields&quot;: [
      { &quot;name&quot;: &quot;field1&quot;,
        &quot;type&quot;: &quot;text&quot;,
        &quot;subtype&quot;: &quot;autofill&quot;,
        &quot;value&quot;: &quot;someVal&quot; },
      { &quot;name&quot;: &quot;field2&quot;,
        &quot;type&quot;: &quot;text&quot;,
        &quot;subtype&quot;: &quot;password&quot;,
        &quot;value&quot;: &quot;somePass&quot; },
      { &quot;name&quot;: &quot;field3&quot;,
        &quot;type&quot;: &quot;number&quot;,
        &quot;subtype&quot;: &quot;integer&quot;,
        &quot;value&quot;: 12 },
      { &quot;name&quot;: &quot;field4&quot;,
        &quot;type&quot;: &quot;number&quot;,
        &quot;subtype&quot;: &quot;double&quot;,
        &quot;value&quot;: 3.14 }
    ],
    &quot;etc&quot;: &quot;etc&quot;
  }
  </code></pre>
  <p>The freezed model should be something like this:</p>
  <pre class="lang-dart prettyprint-override"><code>@freezed
  class ExampleSuperclass with _$ExampleSuperclass {
    factory ExampleSuperclass({
      String? name,
      List&lt;Field&gt;? fields,
      String? etc,
    }) = _ExampleSuperclass;
  
    factory ExampleSuperclass.fromJson(Map&lt;String, dynamic&gt; json) =&gt;
        _$ExampleSuperclassFromJson(json);
  }
  
  @Freezed(unionKey: 'type')
  class Field with _$Field {
    factory Field({
      String? name,
      String? type,
      String? subtype,
    }) = _Field;
  
    factory Field.text({
      String? name,
      String? type,
      String? subtype,
    }) = _FieldText;
  
    factory Field.number({
      String? name,
      String? type,
      String? subtype,
    }) = _FieldNumber;
  
    factory Field.fromJson(Map&lt;String, dynamic&gt; json) =&gt; _$FieldFromJson(json);
  }
  </code></pre>
  <p>But the you realize that you need both type and subtype in order to make more subtypes of Field.number and Field.text, how can you achieve a custom fromJson to have that done?</p>