skip to Main Content

I have a big enum data like

    enum LargeCategoryEnum{
      numberOne, numberTwo ; 

    factory LargeCategoryEnum.fromJson(String json) =>
      values.byName(json);
 }

and I want to get this enum from a json response but the problem is that the enum in the json response can be all lowercase or all uppercase. for example it can be NumberOne.
So in the LargeCategoryEnum.fromJson it can not map it to any enum name and fails. how can I parse this json?

3

Answers


  1. Try the following snippet:

    enum Test{
      
      numberOne,
      numberTwo;
      
      factory Test.fromJson(String name)=>
        
        switch(name.toLowerCase()){
            
          'numberone' => Test.numberOne,
            'numbertwo'=>Test.numberTwo,
            _ => Test.numberOne, // it's a must (exhaustive checking)
        };
      
    }
    
    void main() {
    
      print(Test.fromJson('NumberTWO'));
      
    }
    

    which prints:

    Test.numberTwo
    

    Switch over the lowercase would be good for small number of values in an enumerated type, while the values number increases you can use the following:

    enum Test {
      numberOne,
      numberTwo,
      numberThree,
      numberFour,
      numberFive,
      numberSix,
      numberSeven;
    
      factory Test.fromJson(String json) {
        String tempName = '';
    
        values.forEach((e) {
          if ((e.name.toLowerCase() == json.toLowerCase())) {
            tempName = e.name;
          }
        });
    
        if(tempName.isEmpty) {
         throw ArgumentError('Incorrect JSON Value : ' , json);
         }
        return values.byName(tempName);
      }
    }
    
    void main() {
      print(Test.fromJson('NumberOnE'));
      print(Test.fromJson('NumberTWO'));
      print(Test.fromJson('NumBERTHREE'));
      print(Test.fromJson('NUMBERFOUR'));
      print(Test.fromJson('Numberfive'));
      print(Test.fromJson('NumbeRSIX'));
      print(Test.fromJson('NumberSevEN'));
    
      print('end');
    }
    

    which prints:

    Test.numberOne
    Test.numberTwo
    Test.numberThree
    Test.numberFour
    Test.numberFive
    Test.numberSix
    Test.numberSeven
    end
    
    Login or Signup to reply.
  2. Test.values gives you all the values, so you can compare your string with item.name:

    void main() {
      final value = Test.parse("FIVE");
      
      print(value);
    }
    
    
    enum Test {
      one,
      two,
      three,
      four,
      five;
    
      factory Test.parse(String value) {
        for (final item in Test.values) {
          if (item.name.toLowerCase() == value.toLowerCase()) {
            return item;
          }
        }
    
        throw ArgumentError(value);
      }
    }
    
    Login or Signup to reply.
  3. My dartbag package has a ParseEnum extension that would let you do:

    enum LargeCategoryEnum{
      numberOne,
      numberTwo;
    
      factory LargeCategoryEnum.fromJson(String json) {
        var value = values.tryParse(json, caseSensitive: false);
        if (value == null) {
          throw FormatException('Failed to parse: $json');
        }
        return value;
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search