Super man Super man - 2 months ago 6
Android Question

How to re-design this firebase database to use minimal classes?

I am a newbie to Firebase. And I've set up my database, and its working fine. However, there are some issues. My database as JSON is provided below. Kindly have a look at it.

{
"Cohesion" :
{
"Situational" :
{
"Home" :
{
"Chelsea" :
{
"Counter_Attack" : 0,
"Open_Play" : 4,
"Penalty" : 1,
"Set_Piece" : 0,
"Own_goal" : 0
},
"Liverpool" :
{
"Counter_Attack" : 0,
"Open_Play" : 4,
"Penalty" : 0,
"Set_Piece" : 0,
"Own_goal" : 0
},
"Burnley" :
{
"Counter_Attack" : 0,
"Open_Play" : 3,
"Penalty" : 0,
"Set_Piece" : 0,
"Own_goal" : 0
},
"Arsenal" :
{
"Counter_Attack" : 0,
"Open_Play" : 2,
"Penalty" : 1,
"Set_Piece" : 2,
"Own_goal" : 0
},
"Watford" :
{
"Counter_Attack" : 0,
"Open_Play" : 2,
"Penalty" : 0,
"Set_Piece" : 0,
"Own_goal" : 0
},
"West_Ham" :
{
"Counter_Attack" : 0,
"Open_Play" : 2,
"Penalty" : 0,
"Set_Piece" : 1,
"Own_goal" : 0
},
"Bournemouth" :
{
"Counter_Attack" : 0,
"Open_Play" : 2,
"Penalty" : 0,
"Set_Piece" : 0,
"Own_goal" : 0
},
"Manchester_City" :
{
"Counter_Attack" : 0,
"Open_Play" : 2,
"Penalty" : 1,
"Set_Piece" : 1,
"Own_goal" : 1
},
"Southampton" :
{
"Counter_Attack" : 0,
"Open_Play" : 1,
"Penalty" : 0,
"Set_Piece" : 1,
"Own_goal" : 0
},
"Middlesbrough" :
{
"Counter_Attack" : 0,
"Open_Play" : 1,
"Penalty" : 0,
"Set_Piece" : 1,
"Own_goal" : 0
},
"Leicester" :
{
"Counter_Attack" : 0,
"Open_Play" : 1,
"Penalty" : 0,
"Set_Piece" : 1,
"Own_goal" : 0
},
"Swansea" :
{
"Counter_Attack" : 0,
"Open_Play" : 1,
"Penalty" : 1,
"Set_Piece" : 0,
"Own_goal" : 0
},
"Sunderland" :
{
"Counter_Attack" : 0,
"Open_Play" : 1,
"Penalty" : 0,
"Set_Piece" : 0,
"Own_goal" : 0
},
"Tottenham" :
{
"Counter_Attack" : 0,
"Open_Play" : 1,
"Penalty" : 0,
"Set_Piece" : 1,
"Own_goal" : 0
},
"Crystal_Palace" :
{
"Counter_Attack" : 0,
"Open_Play" : 1,
"Penalty" : 0,
"Set_Piece" : 0,
"Own_goal" : 0
},
"Manchester_United" :
{
"Counter_Attack" : 0,
"Open_Play" : 1,
"Penalty" : 1,
"Set_Piece" : 1,
"Own_goal" : 0
},
"Hull" :
{
"Counter_Attack" : 0,
"Open_Play" :1,
"Penalty" : 0,
"Set_Piece" : 1,
"Own_goal" : 0
},
"West_Bromwich_Albion" :
{
"Counter_Attack" : 0,
"Open_Play" : 0,
"Penalty" : 0,
"Set_Piece" : 1,
"Own_goal" : 0
},
"Stoke" :
{
"Counter_Attack" : 0,
"Open_Play" : 0,
"Penalty" : 1,
"Set_Piece" : 0,
"Own_goal" : 0
},
"Everton" :
{
"Counter_Attack" : 0,
"Open_Play" : 0,
"Penalty" : 0,
"Set_Piece" : 1,
"Own_goal" : 1
}
},
"Away" :
{
"Chelsea" :
{
"Counter_Attack" : 1,
"Open_Play" : 3,
"Penalty" : 0,
"Set_Piece" : 0,
"Own_goal" : 0
},
"Liverpool" :
{
"Counter_Attack" : 0,
"Open_Play" : 3,
"Penalty" : 0,
"Set_Piece" : 1,
"Own_goal" : 1
},
"Burnley" :
{
"Counter_Attack" : 0,
"Open_Play" : 0,
"Penalty" : 0,
"Set_Piece" : 0,
"Own_goal" : 0
},
"Arsenal" :
{
"Counter_Attack" : 0,
"Open_Play" : 2,
"Penalty" : 1,
"Set_Piece" : 0,
"Own_goal" : 0
},
"Watford" :
{
"Counter_Attack" : 0,
"Open_Play" : 5,
"Penalty" : 0,
"Set_Piece" : 0,
"Own_goal" : 0
},
"West_Ham" :
{
"Counter_Attack" : 0,
"Open_Play" : 1,
"Penalty" : 0,
"Set_Piece" : 1,
"Own_goal" : 0
},
"Bournemouth" :
{
"Counter_Attack" : 0,
"Open_Play" : 0,
"Penalty" : 0,
"Set_Piece" : 1,
"Own_goal" : 0
},
"Manchester_City" :
{
"Counter_Attack" : 0,
"Open_Play" : 4,
"Penalty" : 1,
"Set_Piece" : 1,
"Own_goal" : 1
},
"Southampton" :
{
"Counter_Attack" : 0,
"Open_Play" : 0,
"Penalty" : 0,
"Set_Piece" : 0,
"Own_goal" : 1
},
"Middlesbrough" :
{
"Counter_Attack" : 0,
"Open_Play" : 2,
"Penalty" : 0,
"Set_Piece" : 0,
"Own_goal" : 0
},
"Leicester" :
{
"Counter_Attack" : 0,
"Open_Play" : 1,
"Penalty" : 1,
"Set_Piece" : 0,
"Own_goal" : 0
},
"Swansea" :
{
"Counter_Attack" : 0,
"Open_Play" : 2,
"Penalty" : 0,
"Set_Piece" : 0,
"Own_goal" : 0
},
"Sunderland" :
{
"Counter_Attack" : 0,
"Open_Play" : 1,
"Penalty" : 1,
"Set_Piece" : 0,
"Own_goal" : 0
},
"Tottenham" :
{
"Counter_Attack" : 1,
"Open_Play" : 4,
"Penalty" : 0,
"Set_Piece" : 1,
"Own_goal" : 0
},
"Crystal_Palace" :
{
"Counter_Attack" : 0,
"Open_Play" : 2,
"Penalty" : 0,
"Set_Piece" : 0,
"Own_goal" : 0
},
"Manchester_United" :
{
"Counter_Attack" : 0,
"Open_Play" : 4,
"Penalty" : 0,
"Set_Piece" : 0,
"Own_goal" : 0
},
"Hull" :
{
"Counter_Attack" : 0,
"Open_Play" :1,
"Penalty" : 0,
"Set_Piece" : 2,
"Own_goal" : 0
},
"West_Bromwich_Albion" :
{
"Counter_Attack" : 0,
"Open_Play" : 0,
"Penalty" : 0,
"Set_Piece" : 1,
"Own_goal" : 0
},
"Stoke" :
{
"Counter_Attack" : 0,
"Open_Play" : 0,
"Penalty" : 0,
"Set_Piece" : 1,
"Own_goal" : 0
},
"Everton" :
{
"Counter_Attack" : 0,
"Open_Play" : 4,
"Penalty" : 0,
"Set_Piece" : 1,
"Own_goal" : 0
}

}
},
"Summary" : {
"Arsenal" : 7.02,
"Bournemouth" : 6.67,
"Burnley" : 6.71,
"Chelsea" : 6.92,
"Crystal_Palace" : 6.88,
"Everton" : 7.12,
"Hull" : 7.04,
"Leicester" : 6.6,
"Liverpool" : 6.87,
"Manchester_City" : 7.06,
"Manchester_United" : 7.043,
"Middlesbrough" : 6.82,
"Southampton" : 6.57,
"Stoke" : 6.55,
"Sunderland" : 6.52,
"Swansea" : 6.83,
"Tottenham" : 7.01,
"Watford" : 6.7,
"West_Bromwich_Albion" : 6.73,
"West_Ham" : 6.74
},
"Shots" :
{
"Arsenal" :
{
"Total" : 13.0,
"OffTarget" : 5.0,
"OnTarget" : 3.5
},

"Bournemouth" :
{
"Total" : 10.5,
"OffTarget" : 3.5,
"OnTarget" : 4.0
},

"Burnley" :
{
"Total" : 8.7,
"OffTarget" : 4.0,
"OnTarget" : 2.3
},

"Chelsea" :
{
"Total" : 19.0,
"OffTarget" : 6.5,
"OnTarget" : 8.0
},

"Crystal_Palace" :
{
"Total" : 19.0,
"OffTarget" : 9.5,
"OnTarget" : 4.5
},

"Everton" :
{
"Total" : 16.5,
"OffTarget" : 5.5,
"OnTarget" : 7.5
},

"Hull" :
{
"Total" : 11.0,
"OffTarget" : 5.5,
"OnTarget" : 3.5
},

"Leicester" :
{
"Total" : 10.5,
"OffTarget" : 3.5,
"OnTarget" : 3.5
},

"Liverpool" :
{
"Total" : 17.0,
"OffTarget" : 2.0,
"OnTarget" : 11.0

},

"Manchester_City" :
{
"Total" : 19.0,
"OffTarget" : 9.0,
"OnTarget" : 4.5
},

"Manchester_United" :
{
"Total" : 13.0,
"OffTarget" : 6.0,
"OnTarget" : 4
},

"Middlesbrough" :
{
"Total" : 12.0,
"OffTarget" : 5.5,
"OnTarget" : 3.0
},

"Southampton" :
{
"Total" : 20.0,
"OffTarget" : 7.5,
"OnTarget" : 6.5
},

"Stoke" :
{
"Total" : 12.0,
"OffTarget" : 5.0,
"OnTarget" : 2.5
},

"Sunderland" :
{
"Total" : 14.5,
"OffTarget" : 3.5,
"OnTarget" : 3.5
},

"Swansea" :
{
"Total" : 14.5,
"OffTarget" : 7.5,
"OnTarget" : 2.5
},

"Tottenham" :
{
"Total" : 15.5,
"OffTarget" : 7.0,
"OnTarget" : 4.5
},

"Watford" :
{
"Total" : 10.0,
"OffTarget" : 4.0,
"OnTarget" : 4.0
},

"West_Bromwich_Albion" :
{
"Total" : 12.0,
"OffTarget" : 5.5,
"OnTarget" : 3.0
},

"West_Ham" :
{
"Total" : 17.5,
"OffTarget" : 10.0,
"OnTarget" : 3.0
}

},
"Passes" :
{
"Home" :
{
"Arsenal" :
{
"Cross" : 24,
"ThroughBall" : 4,
"LongBall" : 51,
"ShortPass" : 489
},

"Bournemouth" :
{
"Cross" : 26,
"ThroughBall" : 1,
"LongBall" : 65,
"ShortPass" : 420
},

"Burnley" :
{
"Cross" : 15,
"ThroughBall" : 0,
"LongBall" : 77,
"ShortPass" : 245
},

"Chelsea" :
{
"Cross" : 23,
"ThroughBall" : 2,
"LongBall" : 70,
"ShortPass" : 527
},

"Crystal_Palace" :
{
"Cross" : 36,
"ThroughBall" : 0,
"LongBall" : 82,
"ShortPass" : 356
},

"Everton" :
{
"Cross" : 16,
"ThroughBall" : 1,
"LongBall" : 75,
"ShortPass" : 355
},

"Hull" :
{
"Cross" : 11,
"ThroughBall" : 2,
"LongBall" : 86,
"ShortPass" : 336
},

"Leicester" :
{
"Cross" : 17,
"ThroughBall" : 1,
"LongBall" : 73,
"ShortPass" : 315
},

"Liverpool" :
{
"Cross" : 7,
"ThroughBall" : 3,
"LongBall" : 75,
"ShortPass" : 534
},

"Manchester_City" :
{
"Cross" : 20,
"ThroughBall" : 4,
"LongBall" : 56,
"ShortPass" : 579
},

"Manchester_United" :
{
"Cross" : 22,
"ThroughBall" : 1,
"LongBall" : 57,
"ShortPass" : 341
},

"Middlesbrough" :
{
"Cross" : 28,
"ThroughBall" : 0,
"LongBall" : 74,
"ShortPass" : 368
},

"Southampton" :
{
"Cross" : 26,
"ThroughBall" : 3,
"LongBall" : 51,
"ShortPass" : 486
},

"Stoke" :
{
"Cross" : 20,
"ThroughBall" : 1,
"LongBall" : 67,
"ShortPass" : 283
},

"Sunderland" :
{
"Cross" : 17,
"ThroughBall" : 2,
"LongBall" : 54,
"ShortPass" : 291
},

"Swansea" :
{
"Cross" : 27,
"ThroughBall" : 0,
"LongBall" : 51,
"ShortPass" : 437
},

"Tottenham" :
{
"Cross" : 20,
"ThroughBall" : 2,
"LongBall" : 76,
"ShortPass" : 361
},

"Watford" :
{
"Cross" : 20,
"ThroughBall" : 1,
"LongBall" : 68,
"ShortPass" : 305
},

"West_Bromwich_Albion" :
{
"Cross" : 20,
"ThroughBall" : 0,
"LongBall" : 67,
"ShortPass" : 253
},

"West_Ham" :
{
"Cross" : 24,
"ThroughBall" : 1,
"LongBall" : 58,
"ShortPass" : 345
}

},

"Away" :
{
"Arsenal" :
{
"Cross" : 19,
"ThroughBall" : 5,
"LongBall" : 55,
"ShortPass" : 474
},

"Bournemouth" :
{
"Cross" : 14,
"ThroughBall" : 2,
"LongBall" : 76,
"ShortPass" : 296
},

"Burnley" :
{
"Cross" : 20,
"ThroughBall" : 0,
"LongBall" : 54,
"ShortPass" : 365
},

"Chelsea" :
{
"Cross" : 24,
"ThroughBall" : 2,
"LongBall" : 55,
"ShortPass" : 474
},

"Crystal_Palace" :
{
"Cross" : 24,
"ThroughBall" : 1,
"LongBall" : 73,
"ShortPass" : 287
},

"Everton" :
{
"Cross" : 19,
"ThroughBall" : 2,
"LongBall" : 79,
"ShortPass" : 500
},

"Hull" :
{
"Cross" : 16,
"ThroughBall" : 3,
"LongBall" : 84,
"ShortPass" : 460
},

"Leicester" :
{
"Cross" : 17,
"ThroughBall" : 1,
"LongBall" : 67,
"ShortPass" : 339
},

"Liverpool" :
{
"Cross" : 17,
"ThroughBall" : 2,
"LongBall" : 66,
"ShortPass" : 528
},

"Manchester_City" :
{
"Cross" : 15,
"ThroughBall" : 5,
"LongBall" : 63,
"ShortPass" : 482
},

"Manchester_United" :
{
"Cross" : 24,
"ThroughBall" : 1,
"LongBall" : 61,
"ShortPass" : 531
},

"Middlesbrough" :
{
"Cross" : 13,
"ThroughBall" : 3,
"LongBall" : 90,
"ShortPass" : 407
},

"Southampton" :
{
"Cross" : 20,
"ThroughBall" : 1,
"LongBall" : 72,
"ShortPass" : 427
},

"Stoke" :
{
"Cross" : 20,
"ThroughBall" : 1,
"LongBall" : 56,
"ShortPass" : 357
},

"Sunderland" :
{
"Cross" : 18,
"ThroughBall" : 2,
"LongBall" : 62,
"ShortPass" : 171
},

"Swansea" :
{
"Cross" : 26,
"ThroughBall" : 0,
"LongBall" : 67,
"ShortPass" : 314
},

"Tottenham" :
{
"Cross" : 20,
"ThroughBall" : 0,
"LongBall" : 78,
"ShortPass" : 423
},

"Watford" :
{
"Cross" : 14,
"ThroughBall" : 1,
"LongBall" : 72,
"ShortPass" : 258
},

"West_Bromwich_Albion" :
{
"Cross" : 19,
"ThroughBall" : 1,
"LongBall" : 81,
"ShortPass" : 182
},

"West_Ham" :
{
"Cross" : 13,
"ThroughBall" : 1,
"LongBall" : 83,
"ShortPass" : 235
}

}
},

"Recent" :
{
"Manchester_City" :
{
"Game1" : 1,
"Game2" : 1,
"Game3" : 1,
"Game4" : 1,
"Game5" : 1
},

"Everton" :
{
"Game1" : 0,
"Game2" : 1,
"Game3" : 1,
"Game4" : 1,
"Game5" : 1
},

"Tottenham" :
{
"Game1" : 0,
"Game2" : 1,
"Game3" : 0,
"Game4" : 1,
"Game5" : 1
},

"Arsenal" :
{
"Game1" : -1,
"Game2" : 0,
"Game3" : 1,
"Game4" : 1,
"Game5" : 1
},

"Chelsea" :
{
"Game1" : 1,
"Game2" : 1,
"Game3" : 1,
"Game4" : 0,
"Game5" : 1
},

"Liverpool" :
{
"Game1" : 1,
"Game2" : -1,
"Game3" : 0,
"Game4" : 1,
"Game5" : 1
},

"Manchester_United" :
{
"Game1" : 1,
"Game2" : 1,
"Game3" : 1,
"Game4" : -1,
"Game5" : -1
},

"Crystal_Palace" :
{
"Game1" : -1,
"Game2" : -1,
"Game3" : 0,
"Game4" : 1,
"Game5" : 1
},

"Watford" :
{
"Game1" : 0,
"Game2" : -1,
"Game3" : -1,
"Game4" : 1,
"Game5" : 1
},

"West_Bromwich_Albion" :
{
"Game1" : 1,
"Game2" : -1,
"Game3" : 0,
"Game4" : -1,
"Game5" : 1
},

"Leicester" :
{
"Game1" : -1,
"Game2" : 0,
"Game3" : 1,
"Game4" : -1,
"Game5" : 1
},

"Hull" :
{
"Game1" : 1,
"Game2" : 1,
"Game3" : -1,
"Game4" : 0,
"Game5" : -1
},

"Middlesbrough" :
{
"Game1" : 0,
"Game2" : 1,
"Game3" : 0,
"Game4" : -1,
"Game5" : -1
},

"Southampton" :
{
"Game1" : 0,
"Game2" : -1,
"Game3" : 0,
"Game4" : -1,
"Game5" : 1
},

"Swansea" :
{
"Game1" : 1,
"Game2" : -1,
"Game3" : -1,
"Game4" : 0,
"Game5" : -1
},

"Burnley" :
{
"Game1" : -1,
"Game2" : 1,
"Game3" : -1,
"Game4" : 0,
"Game5" : -1
},

"Bournemouth" :
{
"Game1" : -1,
"Game2" : -1,
"Game3" : 0,
"Game4" : 1,
"Game5" : -1
},

"West_Ham" :
{
"Game1" : -1,
"Game2" : 1,
"Game3" : -1,
"Game4" : -1,
"Game5" : -1
},

"Sunderland" :
{
"Game1" : -1,
"Game2" : -1,
"Game3" : 0,
"Game4" : -1,
"Game5" : -1
},

"Stoke" :
{
"Game1" : 0,
"Game2" : -1,
"Game3" : -1,
"Game4" : -1,
"Game5" : -1
}
},
"Head2Head" :
{

}
}
}


This is a database that consist the data of English premier league teams. Now since Firebase uses Jackson to deserialize JSON, I have a big issue here, I'd need to create lot many classes to access these data, So one solution I was thinking was to club as many values possible under one club name, which is repeated here. That'd minimize the usage of redundant classes, however I would like to know if there's any tweaks I can apply over Firebase to access these values with ease.

But there's another issue, ie, I also need to store
head2head
values, ie the stats when two teams faced each other, since there are 20 teams, it will have 380 games and their associated stats. If I were to do this regular way,ie:

"Head2Head" :
{
"M1X" :
{
"Team1": 290,
"Team2": 180,
"Draw" : 30
}
}


Which would force to create around 380 Java classes. Am I missing any common tweak that'd reduce my task in very minimal way. Is there anything I can do over Firebase to address an issue like this? Kindly help.

Answer

Overall, you need to move the "key" into the object, and give each class a "name" field.

Class 1: A Team.

"Chelsea" : 
    {
      "Counter_Attack" : 0,
      "Open_Play" : 4,
      "Penalty" : 1,
      "Set_Piece" : 0,
      "Own_goal" : 0
    },
    "Liverpool" : 
    {
      "Counter_Attack" : 0,
      "Open_Play" : 4,
      "Penalty" : 0,
      "Set_Piece" : 0,
      "Own_goal" : 0
    },

Becomes this for "home" and "away"

"home": [
   { "Name": "Chelsea", "Open_Play": 4, ...}, 
   { "Name": "Liverpool", "Open_Play": 4, ...},
   ...
]

Class 2: Summary looks fine.


Class 3: Shot

  "Arsenal" :
    {
      "Total" : 13.0,
      "OffTarget" : 5.0,
      "OnTarget" : 3.5
    },

    "Bournemouth" :
    {
      "Total" : 10.5,
      "OffTarget" : 3.5,
      "OnTarget" : 4.0
    },

Becomes this

"shots": [
   {"Name": "Arsenal", "Total": 13.0, ...}, 
   {"Name": "Bournemouth", "Total": 10.5, ...},
   ...
]

Class 4: Passes - same idea


Class 5: Recent

  "Manchester_City" :
  {
    "Game1" : 1,
    "Game2" : 1,
    "Game3" : 1,
    "Game4" : 1,
    "Game5" : 1
  },
  "Everton" :
  {
    "Game1" : 0,
    "Game2" : 1,
    "Game3" : 1,
    "Game4" : 1,
    "Game5" : 1
  },

The games might be better represented as a list

"recents": [
    {"Name": "Manchester_City", "games": [1, 1, 1, 1, 1]},
    {"Name": "Everton", "games": [0, 1, 1, 1, 1]}, 
    ...
]