I’m in love and obsessed with JSON. I’m working with node.js and mongodb, and I’m torn between two different philosophies.
1
{
"app":{
"keys":{
"facebook":{
"apikey":"1412v5l1v5jv5j1h2v5",
"sharedsecret":"v5j12hv51hc4v123vmnv",
},
"twitter":{
"apikey":"3241bly5vlv1l2hjv51",
"sharedsecret":"gxdz1n25f1m235xm1235",
}
}
}
}
2
{
"app":{
"keys":{
"facebook_apikey":"1412v5l1v5jv5j1h2v5",
"facebook_sharedsecret":"v5j12hv51hc4v123vmnv",
"twitter_apikey":"3241bly5vlv1l2hjv51",
"twitter_sharedsecret":"gxdz1n25f1m235xm1235",
}
}
}
3 or even
{
"app":{
"facebook_apikey":"1412v5l1v5jv5j1h2v5",
"facebook_sharedsecret":"v5j12hv51hc4v123vmnv",
"twitter_apikey":"3241bly5vlv1l2hjv51",
"twitter_sharedsecret":"gxdz1n25f1m235xm1235",
}
}
To make the data even more complex
{
"app":{
"keys":{
"facebook":{
"production":{
"apikey":"1412v5l1v5jv5j1h2v5",
"sharedsecret":"v5j12hv51hc4v123vmnv",
},
"development":{
"apikey":"1412v5l1v5jv5j1h2v5",
"sharedsecret":"v5j12hv51hc4v123vmnv",
},
},
"twitter":{
"production":{
"apikey":"1412v5l1v5jv5j1h2v5",
"sharedsecret":"v5j12hv51hc4v123vmnv",
},
"development":{
"apikey":"1412v5l1v5jv5j1h2v5",
"sharedsecret":"v5j12hv51hc4v123vmnv",
},
}
}
}
}
alternatively
{
"app":{
"keys":{
"production":{
"facebook":{
"apikey":"1412v5l1v5jv5j1h2v5",
"sharedsecret":"v5j12hv51hc4v123vmnv",
},
"twitter":{
"apikey":"1412v5l1v5jv5j1h2v5",
"sharedsecret":"v5j12hv51hc4v123vmnv",
},
},
"development":{
"facebook":{
"apikey":"1412v5l1v5jv5j1h2v5",
"sharedsecret":"v5j12hv51hc4v123vmnv",
},
"twitter":{
"apikey":"1412v5l1v5jv5j1h2v5",
"sharedsecret":"v5j12hv51hc4v123vmnv",
},
},
}
}
}
How deep should you go? Is there a thing as too far in or to far out?
I’d go with the following, and in fact did go with something very similar to the following:
When I design a system I look for to create common code that handles multiple different situations. This typically means creating a consistency in the design.
The above allows you to create a routine that finds the “app” and then choose an operating environment, either “production” or “development”. That can then be handed to another function and asked to find the specific service you care about, either “facebook”, “twitter”, or something new like “foursquare”. And then a single oAuth set of functions can handle the authorization process no matter the passed in object because it will consistently be able to ask for “apikey” and “sharesecret” without regard to which service is being used.
Speaking to the flexibility of Mongo or even Javascript… I love the flexibility. It allows us to efficiently solve problems that would be more difficult in other toolsets. However, that flexibility needs to be tempered with as much consistency as you can muster if you are going to get code efficiency and ease of debugging.