~oubiwann/txjsonrpc/0.0.3

« back to all changes in this revision

Viewing changes to adytum/twisted/protocols/json.py

  • Committer: oubiwann
  • Date: 2006-05-06 09:20:28 UTC
  • Revision ID: svn-v3-trunk0:4a8ffc7c-3f36-0410-a8b2-296a659b5ce7:trunk:9
2006.05.06

* Added a beginning sketch of JSON over TCP in protocols.json.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import simplejson
 
2
 
 
3
from twisted.internet import protocol 
 
4
from twisted.protocols import basic
 
5
 
 
6
class Error(Exception):
 
7
    pass
 
8
 
 
9
class JSONParseError(Error):
 
10
    pass
 
11
 
 
12
class JSONStream(object):
 
13
 
 
14
    def parse(self, buffer):
 
15
        try:
 
16
            # XXX
 
17
            simplejson.loads(buffer)
 
18
        except JSONParseError, e:
 
19
            raise JSONParseError, str(e)
 
20
 
 
21
class JSONLineReceiver(basic.LineReceiver):
 
22
 
 
23
    def __init__(self):
 
24
        basic.LineReceiver.__init__(self)
 
25
        self.stream = None
 
26
 
 
27
    def connectionMade(self):
 
28
        self.stream = JSONStream()
 
29
 
 
30
    def rawDataReceived(self, data):
 
31
        try:
 
32
            self.stream.parse(data)
 
33
        except JSONParseError:
 
34
            self.transport.loseConnection()
 
35
 
 
36
    def connectionLost(self, reason):
 
37
        self.stream = None
 
38
            
 
39
class JSONRawReceiverFactory(protocol.Protocol)
 
40
 
 
41
    def buildProtocol(self, addr):
 
42
        p = JSONLineReceiver()
 
43
        p.setRawMode()
 
44
        p.factory = self
 
45
        return p