Serializing PHP objects into JSON with JsonSerializable

August 29, 2012 | 3 comments

JSON (JavaScript Object Notation) is a handy data interchange format that’s overtaken XML in a very short time for a lot of API use cases – and rightly so. It’s easy to use, bandwidth efficient, and widely supported across platforms and languages.

PHP is still one of the most widely-used web scripting languages. It’s perhaps not as cool as Python or Ruby on Rails, but it’s got its adherents. I’m still a fan, although there are some things – like long-polling real-time sites – that are much harder to do on the platform. And when it comes to JSON, it doesn’t get any easier than $json_string = json_encode($php_data); and $php_data = json_decode($json_string);. (Assuming you have the PHP JSON module compiled and enabled – but if you’re using PHP 5.2 or above, you probably do.)

Unfortunately, if you’re using object orientated code and your data to encode includes an object, json_encode() has traditionally not been as friendly as it might have been. Sometimes, for example, you want to define exactly how your objects translate to JSON, to avoid lost data (or too much data). This is particularly true if you’re using magic methods to get and set properties, as I often do.

PHP 5.4 contains a new interface, JsonSerializable, that gives you this control. Once you add the interface to your class:

class myClass implements JsonSerializable {
    // ...
}

You can add a new function, jsonSerialize, that will be triggered whenever json_encode() is run on the object, and returns the PHP data of your choice:

class myClass implements JsonSerializable {
    // ...
    
    function jsonSerialize() {
        $data = array();
        // Represent your object using a nested array or stdClass,
        // in the way you want it arranged in your API
        return $data;
    }
}

Now, when you run json_encode() over a data structure that includes your objects, you’ll know that they’re being saved in a way that can easily be interpreted by your API consumers. Again, the function should return PHP data that can be serialized by json_encode(), rather than encoded JSON data. I recommend using nested arrays, or a stdClass object, for simplicity.

Note that unlike, say, serialize() (which serializes data in a PHP-specific format), this isn’t a two-way translation. In other words, json_decode() won’t automatically reconstitute your object with class from your data.

We can illustrate that by taking the following test class (which I’ll expand first and then print the var_export() output for):

class TestClass {
    public $foo = 'bar';
    public $dev = 'null';
}
 
TestClass::__set_state(array( 'foo' => 'bar', 'dev' => 'null', ))

Which json_encode() encodes as:

{"foo":"bar","dev":"null"}

(Because that was a simple object, there was no need to employ jsonSerialize() in its construction.)

In turn, the JSON string is decoded back into PHP as:

stdClass::__set_state(array( 'foo' => 'bar', 'dev' => 'null', ))

Note that the classname has vanished and the class is reduced to being a stdClass. As a result, you need to take care when consuming JSON data and turning it back into your internal data structures – which is a very good idea anyway. It’s perhaps worth creating your own validator function that can take in a stdClass object and, following sanity checking and sanitization, turn it back into one of your own classes.

And that’s it. I don’t normally write about code here, so please let me know if you found this useful.

Making billions of dollars from the federated social web

August 28, 2012 | 4 comments

Diaspora was pretty exciting. A Kickstarter campaign that promised it all: a platform created “for everyone to have full control over their data and to empower people in to become responsible, secure, and social Internet dwellers”. They raised over $200,000, which at the time was the largest Kickstarter funding round ever. Neat!

Yesterday, Diaspora announced that they were becoming a community project. That means that the wider open source community will be responsible for its direction; the original team will be involved to some extent, but are now involved in projects like Makr.io, which just made its way through Y Combinator. (Makr.io is released under the same open source license as Diaspora: here’s the GitHub repository.)

The federated social web is a holy grail for many of us. It’s a simple idea: rather than ceding control of our data and activities to a monolithic, monopolistic third party, why don’t we create an open market for services we can use, which can all interoperate? Those of us willing to set up our own servers would own our own data; others would use the servers set up by their schools or employers; others still would join the hosted service of their choice. The result would be a more innovative, web-like application web, where niche interests could be better served and people could choose the applications and interfaces that best suited them.

It goes far beyond Facebook-style social sites: the potential is for deep interoperability between every application on the web and beyond – and, potentially, identity spaces where we could keep our personal information safe.

Back in 2005, I tried to make the case in a letter to the social web hating journalist Andrew Orlowski:

If a thousand sites depend on Flickr, what happens if Flickr goes down? Wouldn’t it make a great deal more sense to think about standards for data transfer and availability to allow for Flickr-like data all over the Internet, rather than in one place – effectively a peer-to-peer network (or networks) of tagged resources? That way one can still grab information and manipulate it, but with zero dependence and a high tolerance for network failure. Rather than there being one nebulous Microsoft Office 12 For Web, there could be ten thousand office applications that all use the same standards and allow for data transfer perhaps with storage services elsewhere. The answer to “how bad do you think it will get” is “exactly the same as now” if we allow one service provider to create a monopoly for a particular type of service. As ever, the solution is open standards with a framework that anyone can use. This is obviously not going to lead to billions of dollars for any one provider, so it’s probably up to the open source movement to create.

Returning to that letter seven years later, I think I was wrong about one thing: I believe it is possible to make billions of dollars from interoperable social web applications – and there’s no need for mass public adoption of apps using federated social web standards to do it.

I’m convinced that business software should be using federation protocols, as long as those protocols have built-in access permissions. Imagine if your organization – a government department, say, or a corporation – could selectively create shared spaces for joint projects with other organizations, for as long as the project existed. Imagine if you could collaborate in more fluid ways, where applications were more like documents that you could introduce into a project when you needed them. Imagine if email was smarter. Whereas decentralization in public consumer apps is an ideological feature, in enterprise apps it has a solid business case and adds measurable value. Businesses will become more profitable by using federated apps.

On hearing the news about Diaspora, StatusNet’s Evan Prodromou made a very sensible plea:

I only have one favour left to ask: please, for the rest of us who are still working full-steam on federated social networks, don’t fall into the comforting fiction that the problem is insurmountable. It’s not; it deserves our attention and support. You didn’t waste the last two years on something pointless and unattainable. Your work matters.

It might sound like hyperbole, but I still believe it could change the way we do business, and ultimately how we all communicate.

Update: In the comments, Johannes Ernst points to his post about why decentralized software is 10x harder. I think he’s right about the problems that need to be overcome. But check out his note at the bottom: watch this space.

Living sustainably in a digital society: is it even possible?

August 23, 2012 | 2 comments

Desert tankI had an interesting conversation about sustainable lifestyles on Twitter this morning.

I believe that climate change is a major threat to our planet, and that we’re in danger of reaching a tipping point beyond which there is no return. More generally, because my personal mission statement is to have a positive impact on the world, I’m conscious that I don’t want to make an unsustainable amount of waste.

Which of course, I do. Although I try to be a good citizen – I consume locally-produced products, I take public transport rather than driving whenever possible – I’m aware of my relatively large carbon footprint. In particular, I fly a fair amount, often across large distances, I run computer servers, which by necessity are switched on all the time, and I buy electronics, which have an appalling environmental impact. (It’s sometimes easy to forget how much of an impact computing has, because it feels clean – the impacts are out of sight, out of mind. Truthiness in consumption.)

I travel less than I could, of course. A lot of my meetings happen over Google+ Hangouts or Skype, which let me see people without having to actually be in the same room with them. BART makes traveling across the bay to San Francisco simple and relatively clean (PDF link). But I do own a car, which I drive two or three times a week, and I will be flying both to XOXO in Portland, and to Austin, next month. It’s by far the cheapest way to get there.

And that’s the kicker. I could run the server that powers this website through a renewably-powered web host like AISO, for example, but I don’t. A dedicated server through a green host is simply much more expensive than through my existing host. Every financial transaction counts, particularly in the midst of a financial crisis, and I need to think about my costs.

Because flying is cheaper than taking the train – a ludicrous state of affairs – people will fly. It’s as simple as that. Making any other decision is solely the domain of people who don’t need to worry about money, and that’s not most of us. Anyone who’s run a startup knows that the bottom line is important. The key is making products and services available that don’t kill the earth, but are also economically competitive. They’ve got to be affordable. That’s the real-world requirement for most people.

A lot of our environmentally unsustainable practices are due to the economies of bulk production. My hope is that the web can help with this: because it’s now easier to connect ourselves together than ever before, and take payments through awesome services like Square, theoretically local producers can make themselves more available to local customers than ever before. There’s less of a need to ship many products – particularly food – from half a world away, or to make many things in very centralized ways. But the reality is also that, without a Stargate at our disposal, materials still need to be shipped. Everything isn’t available everywhere. That’s just how the world is. (I’m lucky that I live in California, which is a major food producer – there are a great deal more local resources than there were, for example, when I lived in Scotland.)

Technology can help in other ways. Meat is unsustainable. I’m not a vegetarian, but I’ve tried Beyond Meat, and I like it. I also like their mission, which is to create a product that is healthier and more environmentally sustainable than meat, but also cheaper. That’s smart, and is exactly the kind of pragmatic thinking that will change peoples’ lifestyles. I don’t think that people are going to be moving from chicken to reclaimed pea protein in droves, at least not immediately, but I respect that they’ve considered peoples’ real-life needs, and aren’t creating an elitist product. (And I do honestly think it’s pretty tasty.)

Ultimately, though, we’ve created a society based around production and consumption, and we’re all active participants. In fact, we love it – and that’s okay. What we can do, though, is understand that a more efficient infrastructure is also more sustainable – both economically and environmentally. Well-built public transport would lower our costs as business people, stimulate economic growth by encouraging trade, and would also lower our emissions. Better datacenters and less wasteful methods of production would reduce our costs, and also reduce the impact of the products and services we consume.

But it also requires longer-term thinking. Products that consume less electricity cost less in the long term, but might have a higher up-front price. We might pay a little more tax as a percentage to get a high-speed rail link up and running, but we will be repaid by the economic benefits. Modern distribution methods require money to install, but they eventually more than pay for themselves. We need a more efficient infrastructure to do business in the 21st century, and it just so happens that establishing that will be better for our planet – which in turn will result in fewer of the negative effects of climate change and resource depletion, and will also leave us correspondingly wealthier.

The politicians and businessmen who stand in the way of this are in it for the short-term, and are in it for themselves. It’s not just that they want to spend the money on infrastructure upgrades – they want to keep economic value locked up legacy gatekeeper structures, and prevent us from doing business on our terms. I’m not sure it’s possible to live sustainably in a digital society, but it can be, and it should be.

We’ve got to keep moving forward.

Tent

August 22, 2012 | 6 comments

Tent appeared out of the blue today: a protocol and reference server implementation for individual-to-individual distributed social networking. Or to put it another way, Tent is a way to host your own social data – posting and reading from as many apps as you want. Here’s their announcement, and here’s the GitHub repository.

The Tent manifesto is right-on:

Every user has the right to freedom of expression.
Free speech is a necessary feature of all open societies. Speech can not be free if communication is centralized or intermediated. Users must be able to say anything to anyone they want on their own terms.

Every user has the right to control their own data.
This includes who can access the data they create and how that data is later used.

Every user has the right to choose and change their social services providers.
This includes the right to negotiate reasonable terms of service collectively or individually.

Of course, this is hardly the first open source social networking product – and many people are already asking why Tent doesn’t use the OStatus protocol. (StatusNet also includes an individual installation mode.) These are valid questions, but while there’s a slight air of Not Invented Here Syndrome, it’s an elegant idea and the API is very clean and simple, which means there’s every chance an app ecosystem will emerge. If any one of those apps is simple and elegant, we may see a very different kind of social networking community begin to develop.

Even more interestingly, I also think there are real commercial implications for this protocol. More on those in another post. For now, my takeaway is: Tent has the potential to disrupt the entire social web.

Next Page »