2022-11-14

Get classes from JSON in Crystal

I have some JSON file like this:

{
    "2": {
        "_id": 2,
        "_date": "Mon Apr 05 2021",
        "_timestamp": 1617654662313,
        "description": "Some text",
        "isStarred": true,
        "boards": [
            "@3.0",
            "@Some-day"
        ],
        "_isTask": false,
        "isComplete": false,
        "inProgress": false,
        "priority": 1
    },
    "7": {
        "_id": 7,
        "_date": "Mon Apr 05 2021",
        "_timestamp": 1617658197721,
        "description": "Some text too",
        "isStarred": false,
        "boards": [
            "@Some-day"
        ],
        "_isTask": false
    }

}

and I want to parse it in my class Entry:

  enum Priority
      Low # 1
      Medium # 2
      High # 3
  end

  class Entry
      include JSON::Serializable
      property _id : UInt32
      property _date : Time
      property _timestamp : UInt64
      property description : String
      property isStarred : Bool
      property boards : Array(String)
      property _isTask : Bool
      property isComplete : Bool
      property inProgress : Bool
      property priority : Priority
  end

When I try to parse using Hash(String, Entry).from_string it does not work: Expected BeginObject but was String



No comments:

Post a Comment