'Create a JSON string describing a patch to be applied.
Args:
events : list of events to be translated into patches
Returns:
str :  JSON string which can be applied to make the given updates to obj'

def create_json_patch_string(self, events):

    from .server.events import ColumnsPatchedEvent, ColumnsStreamedEvent, ModelChangedEvent, RootAddedEvent, RootRemovedEvent, TitleChangedEvent
    references = set()
    json_events = []
    for event in events:
        if (event.document is not self):
            raise ValueError(('Cannot    create    a    patch    using    events    from    a    different    document    ' + repr(event)))
        if isinstance(event, ModelChangedEvent):
            if isinstance(event.hint, ColumnsStreamedEvent):
                json_events.append({'kind': 'ColumnsStreamed', 'column_source': event.hint.column_source.ref, 'data': event.hint.data, 'rollover': event.hint.rollover})
            elif isinstance(event.hint, ColumnsPatchedEvent):
                json_events.append({'kind': 'ColumnsPatched', 'column_source': event.hint.column_source.ref, 'patches': event.hint.patches})
            else:
                value = event.serializable_new
                value_refs = set(collect_models(value))
                if (event.model != value):
                    value_refs.discard(event.model)
                references = references.union(value_refs)
                json_events.append({'kind': 'ModelChanged', 'model': event.model.ref, 'attr': event.attr, 'new': value})
        elif isinstance(event, RootAddedEvent):
            references = references.union(event.model.references())
            json_events.append({'kind': 'RootAdded', 'model': event.model.ref})
        elif isinstance(event, RootRemovedEvent):
            json_events.append({'kind': 'RootRemoved', 'model': event.model.ref})
        elif isinstance(event, TitleChangedEvent):
            json_events.append({'kind': 'TitleChanged', 'title': event.title})
    json = {'events': json_events, 'references': self._references_json(references)}
    return serialize_json(json)
