discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Missing interface that connects NSCoder to NSStream ?


From: Vaisburd, Haim
Subject: Missing interface that connects NSCoder to NSStream ?
Date: Mon, 12 Mar 2007 18:21:38 -0700

Hi list,

I'm trying to use JSON-RPC protocol
(http://json-rpc.org/wiki/specification)
for client-server communication. JSON (JavaScript Object Notation,
http://json.org)
is not really related to JavaScript, it is indeed a version of ACSII
encoding for
simplified property lists (only strings, numbers, arrays and
dictionaries are
allowed, and notation is slightly different).

For the protocol I need to serialize and deserialize these
simplified property lists to and from JSON notation. I'm trying
to select a reasonable interface for that.

Eventually I'm going to read to and write from a socket, therefore
reading from NSInputStream and writing to NSOutputStream seemed the
best choice (thank you, Richard). However, while writing seemed ok,
the NSInputStream reading interface was not immediately usable for
the parser: the method [NSInputStream -read:maxLength:] requires
maxLength
but I do not know where the encoded dictionary ends until I have parsed
it.

Instead I needed an equivalent to getc()/ungetc() functions that works
for NSInputStream and fill the read buffer behind the scene, like
standard
C io does. It seemed easy, too, and I wrote structure called BYTESTREAM
with
related getc/ungetc macros.

My interface then was

   @interface NSObject (JSON)
     - (id) initFromJSONStream: (BYTESTREAM *) str error: (NSString **)
err;
     - (void) writeToJSONStream: (BYTESTREAM *)str;
   @end

etc. for concrete propery list types.

But yestarday I learned about NSCoder and Cocoa serialization. Now it
seems that
the proper way is to write a corresponding MyJSONDecoder as a subclass
of NSCoder
that can be initialized from NSInputStream.

I was surprised that existing NSCoder subclasses (closest are NSArchiver
and NSDearchiver)
can only be initialized from NSData, but not NSInput/OutputStream.
I understand that not every object in every notation can be deserialized
in one pass,
but JSON was specifically designed for that.

Is there anything wrong with an abstract NSStreamEncoder/NSStreamDecoder
that
can be initialized from corresponding NSStreams:


#define str_getc( str ) ...     // works with ivars
#define str_ungets( str ) ... // works with ivars

@interface NSStreamEncoder : NSCoder
{
  // ivars
}
 - (id) initFromOutputStream: (NSOutputStream *) str; @end
@end

#define str_putc( ch, str )     ...     // works with ivars

@interface NSStreamDecoder : NSCoder
{
  // ivars
}
 - (id) initFromInputStream: (NSInputStream *) str; @end
@end

It's hard to believe Next people did not think about those needs.
Am I missing something?

Thank you very much and sorry for the lengthy message
(and maybe a dumb question).
--Tima




reply via email to

[Prev in Thread] Current Thread [Next in Thread]