Re: Serializing NSDictionary for network transfer

2014-05-08 Thread Manoah F. Adams


Hi,
Thats great.
I first learned about this method from Mike Ash's blog post :  
.

You will likely many more ideas there.

Cheers!


Manoah F. Adams
federaladamsfamily.com/developer

===


On May 8, 2014, at 11:15 , Carl Hoefs wrote:


Wonderful! Just what I was looking for!
Thanks!
-Carl

On May 7, 2014, at 3:13 PM, Manoah F. Adams  
 wrote:



On May 7, 2014, at 12:30 , Carl Hoefs wrote:

For the moment, I'm using only NSStrings and NSNumbers. I'm  
sending data back and forth between OSX and iOS devices, and  
NSDictionary is a very convenient container. Once I show that  
this will work then the pressure will ease off and I'll have time  
dig into more generalized ways (NSPropertyListSerialization?),  
ensuring things won't break in the future when requirements and  
use cases change. I just wasn't prepared going into this for the  
steep learning curve for what I assumed was a more or less  
trivial data serialization function. The documentation (which,  
yes, I have read but still can't make much sense of) documents  
but doesn't educate very well. I'm still a bit overwhelmed by the  
sometimes huge generalized mechanisms required to do things the  
Cocoa way, but I will overcome that with time and effort.


-Carl




Hi,

If you are going Cocoa to Cocoa, and simple data types,  
NSPropertyListSerialization should work.

I use it all the time for reading and writing to disk.

here's an adaptation...

/* transfer out */
NSDictionary* dictionaryToTransfer; // <- set this to your input  
dictionary

NSString* err;
NSData* data = [NSPropertyListSerialization  
dataFromPropertyList:dictionaryToTransfer
  format:NSPropertyListBinaryFormat_v1_0  // or  
NSPropertyListXMLFormat_v1_0

errorDescription:&err];
// now send the data



/* receive */
NSString* err;
NSData* data; // <- set this to your input data from the transfer
NSDictionary* dictionaryToReceive = [NSPropertyListSerialization  
propertyListFromData:data


mutabilityOption:NSPropertyListImmutable
		format:NULL // NSPropertyListSerialization determines for itself  
if the input was as text or as binary.

errorDescription:&err];



You can also use the same procedure for any other property-list  
object (NSArray, NSString, NSNumber, etc.).
(Also check for and adapt the newer api's  
(dataWithPropertyList:format:options:error:   for instance) ).



Manoah F. Adams

This message is signed with a certificate and-or PGP key.
Disregard any unverified messages from this address.
===











smime.p7s
Description: S/MIME cryptographic signature
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Serializing NSDictionary for network transfer

2014-05-07 Thread Manoah F. Adams

On May 7, 2014, at 12:30 , Carl Hoefs wrote:

For the moment, I'm using only NSStrings and NSNumbers. I'm sending  
data back and forth between OSX and iOS devices, and NSDictionary  
is a very convenient container. Once I show that this will work  
then the pressure will ease off and I'll have time dig into more  
generalized ways (NSPropertyListSerialization?), ensuring things  
won't break in the future when requirements and use cases change. I  
just wasn't prepared going into this for the steep learning curve  
for what I assumed was a more or less trivial data serialization  
function. The documentation (which, yes, I have read but still  
can't make much sense of) documents but doesn't educate very well.  
I'm still a bit overwhelmed by the sometimes huge generalized  
mechanisms required to do things the Cocoa way, but I will overcome  
that with time and effort.


-Carl




Hi,

If you are going Cocoa to Cocoa, and simple data types,  
NSPropertyListSerialization should work.

I use it all the time for reading and writing to disk.

here's an adaptation...

/* transfer out */
NSDictionary* dictionaryToTransfer; // <- set this to your input  
dictionary

NSString* err;
NSData* data = [NSPropertyListSerialization  
dataFromPropertyList:dictionaryToTransfer
  format:NSPropertyListBinaryFormat_v1_0  // or  
NSPropertyListXMLFormat_v1_0

errorDescription:&err];
// now send the data



/* receive */
NSString* err;
NSData* data; // <- set this to your input data from the transfer
NSDictionary* dictionaryToReceive = [NSPropertyListSerialization  
propertyListFromData:data


mutabilityOption:NSPropertyListImmutable
		format:NULL // NSPropertyListSerialization determines for itself if  
the input was as text or as binary.

errorDescription:&err];



You can also use the same procedure for any other property-list  
object (NSArray, NSString, NSNumber, etc.).
(Also check for and adapt the newer api's  
(dataWithPropertyList:format:options:error:   for instance) ).



Manoah F. Adams

This message is signed with a certificate and-or PGP key.
Disregard any unverified messages from this address.
===






smime.p7s
Description: S/MIME cryptographic signature
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Serializing NSDictionary for network transfer

2014-05-07 Thread Carl Hoefs
On May 7, 2014, at 12:06 PM, Alex Zavatone  wrote:

> If your dictionary has only text values, this should be no problem with the 
> NSJSONSerialization, but if you're sending images, you'll need to convert the 
> images to 16 bit encoded NSData objects.
> 
> I guess the bigger question is, "what are the data types within your 
> dictionary"?
> 
> If it's just text, then you should have good luck with NSJSONSerialization.
> 
> Another question is, "where are you sending that data to?"  What type of 
> service will receive it?
> 
For the moment, I'm using only NSStrings and NSNumbers. I'm sending data back 
and forth between OSX and iOS devices, and NSDictionary is a very convenient 
container. Once I show that this will work then the pressure will ease off and 
I'll have time dig into more generalized ways (NSPropertyListSerialization?), 
ensuring things won't break in the future when requirements and use cases 
change. I just wasn't prepared going into this for the steep learning curve for 
what I assumed was a more or less trivial data serialization function. The 
documentation (which, yes, I have read but still can't make much sense of) 
documents but doesn't educate very well. I'm still a bit overwhelmed by the 
sometimes huge generalized mechanisms required to do things the Cocoa way, but 
I will overcome that with time and effort.

-Carl


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Serializing NSDictionary for network transfer

2014-05-07 Thread SevenBits
On Wednesday, May 7, 2014, Carl Hoefs 
wrote:

>
> On May 7, 2014, at 11:38 AM, Wim Lewis >
> wrote:
>
> > Depending on what is *in* your NSDictionary, though, a less opaque
> serialization format might be better, such as one of the property-list
> formats (see NSPropertyListSerialization) or even JSON. These formats can
> only hold a small, non-extensible set of types, and can't encode recursive
> structures or preserve the sharedness of parts of the object graph, but
> have the significant advantage that they are easier to inspect and are
> easier to keep decoupled from implementation details of your app.
>
> I realize I have a ways to go before I grok Cocoa, as I still approach
> things from the minimalist Linux perspective. It's a simple NSDictionary
> with a few strings and numbers in it, so maybe (for the moment) the JSON
> approach might get me going, then I can revisit the issue with a more
> general Cocoa solution.


That will work only as long the types in the NSDictionary are textual types
representable in JSON, as previously stated. If even one element is not,
the archiving will fail. I prefer to archive to binary data because usually
the user never has to read the serialized data in its raw form, plus it
avoids the disadvantages of non-extensibility and no recursion.


>
> Thanks!
> -Carl
>
>
> ___
>
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com )
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/sevenbitstech%40gmail.com
>
> This email sent to sevenbitst...@gmail.com 
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Serializing NSDictionary for network transfer

2014-05-07 Thread Alex Zavatone
If your dictionary has only text values, this should be no problem with the 
NSJSONSerialization, but if you're sending images, you'll need to convert the 
images to 16 bit encoded NSData objects.

I guess the bigger question is, "what are the data types within your 
dictionary"?

If it's just text, then you should have good luck with NSJSONSerialization.

Another question is, "where are you sending that data to?"  What type of 
service will receive it?

Cheers, 
- Alex

On May 7, 2014, at 2:38 PM, Wim Lewis wrote:

> 
> On 7 May 2014, at 11:17 AM, Carl Hoefs wrote:
>> Newb questions re: serializing an NSDictionary for network transfer to 
>> another process. I've read over the Apple documentation, but it seems to 
>> detail the methods involved but not how to use serialization, and some 
>> methods seem to require writing archives or plist files to disk. So, I must 
>> be approaching this all wrong...
>> 
>> (1) I see that NSDictionary has an encoding method:
>> 
>> - (void)encodeWithCoder:(NSCoder *)coder;
>> 
>> but this returns (void), which is puzzling to me. I would expect it to 
>> return (void *) to a malloced region containing the serialization. Where 
>> does the object serialization reside, and how do I access it?
> 
> It is accumulated in the coder. NSCoder is an abstract class; the concrete 
> class you're using (eg NSKeyedArchiver) will have a way to get the serialized 
> data in or out.
> 
> NSKeyedArchiver also has a convenience method, +archivedDataWithRootObject:, 
> for the common case of writing a single object (and recursively any objects 
> it references/contains) and getting the data out.
> 
> Depending on what is *in* your NSDictionary, though, a less opaque 
> serialization format might be better, such as one of the property-list 
> formats (see NSPropertyListSerialization) or even JSON. These formats can 
> only hold a small, non-extensible set of types, and can't encode recursive 
> structures or preserve the sharedness of parts of the object graph, but have 
> the significant advantage that they are easier to inspect and are easier to 
> keep decoupled from implementation details of your app.
> 
> 
> 
> ___
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/zav%40mac.com
> 
> This email sent to z...@mac.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Serializing NSDictionary for network transfer

2014-05-07 Thread Carl Hoefs

On May 7, 2014, at 11:38 AM, Wim Lewis  wrote:

> Depending on what is *in* your NSDictionary, though, a less opaque 
> serialization format might be better, such as one of the property-list 
> formats (see NSPropertyListSerialization) or even JSON. These formats can 
> only hold a small, non-extensible set of types, and can't encode recursive 
> structures or preserve the sharedness of parts of the object graph, but have 
> the significant advantage that they are easier to inspect and are easier to 
> keep decoupled from implementation details of your app.

I realize I have a ways to go before I grok Cocoa, as I still approach things 
from the minimalist Linux perspective. It's a simple NSDictionary with a few 
strings and numbers in it, so maybe (for the moment) the JSON approach might 
get me going, then I can revisit the issue with a more general Cocoa solution.

Thanks!
-Carl


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Serializing NSDictionary for network transfer

2014-05-07 Thread Wim Lewis

On 7 May 2014, at 11:17 AM, Carl Hoefs wrote:
> Newb questions re: serializing an NSDictionary for network transfer to 
> another process. I've read over the Apple documentation, but it seems to 
> detail the methods involved but not how to use serialization, and some 
> methods seem to require writing archives or plist files to disk. So, I must 
> be approaching this all wrong...
> 
> (1) I see that NSDictionary has an encoding method:
> 
> - (void)encodeWithCoder:(NSCoder *)coder;
> 
> but this returns (void), which is puzzling to me. I would expect it to return 
> (void *) to a malloced region containing the serialization. Where does the 
> object serialization reside, and how do I access it?

It is accumulated in the coder. NSCoder is an abstract class; the concrete 
class you're using (eg NSKeyedArchiver) will have a way to get the serialized 
data in or out.

NSKeyedArchiver also has a convenience method, +archivedDataWithRootObject:, 
for the common case of writing a single object (and recursively any objects it 
references/contains) and getting the data out.

Depending on what is *in* your NSDictionary, though, a less opaque 
serialization format might be better, such as one of the property-list formats 
(see NSPropertyListSerialization) or even JSON. These formats can only hold a 
small, non-extensible set of types, and can't encode recursive structures or 
preserve the sharedness of parts of the object graph, but have the significant 
advantage that they are easier to inspect and are easier to keep decoupled from 
implementation details of your app.



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Serializing NSDictionary for network transfer

2014-05-07 Thread Quincey Morris
On May 7, 2014, at 11:17 , Carl Hoefs  wrote:

> (1) I see that NSDictionary has an encoding method:
> 
> - (void)encodeWithCoder:(NSCoder *)coder;
> 
> but this returns (void), which is puzzling to me. I would expect it to return 
> (void *) to a malloced region containing the serialization. Where does the 
> object serialization reside, and how do I access it?

NSKeyedArchiver and NSKeyedUnarchiver are the classes that perform the encoding 
and decoding. They are responsible for invoking ‘encodeWithCoder:’ and 
‘initWithCoder:’ for the objects that make up an archive — you don’t do that 
yourself.

Both NSKeyedArchiver and NSKeyedUnarchiver have convenience class methods for 
archiving and unarchiving an object hierarchy to/from NSData. (You can also 
create instances of NSKeyedArchiver and NSKeyedUnarchiver if you need a finer 
level of control.)

Class NSPropertyListSerialization provides a similar service, but for a more 
restricted kind of object graph, useful when you want a textual representation 
of the data, for example.

You really should read some of the documentation:


https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Archiving/Archiving.html

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html

Just reading the introductory sections should orient you in this Cocoa sub 
universe.

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Serializing NSDictionary for network transfer

2014-05-07 Thread Carl Hoefs
On May 7, 2014, at 11:21 AM, Joseph Dixon  wrote:

> Consider NSJSONSerialization.

Hmm, I hadn't come across NSJSONSerialization. Looks straightforward to use.
Thanks!
-Carl

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Serializing NSDictionary for network transfer

2014-05-07 Thread Joseph Dixon
Consider NSJSONSerialization.   

-jwd// Joseph W. Dixon

OS X 10.9
Newb questions re: serializing an NSDictionary for network transfer to another 
process. I've read over the Apple documentation, but it seems to detail the 
methods involved but not how to use serialization, and some methods seem to 
require writing archives or plist files to disk. So, I must be approaching this 
all wrong...

(1) I see that NSDictionary has an encoding method:

- (void)encodeWithCoder:(NSCoder *)coder;

but this returns (void), which is puzzling to me. I would expect it to return 
(void *) to a malloced region containing the serialization. Where does the 
object serialization reside, and how do I access it?

(2) On the receiving end, I see that NSDictionary has the method:

- (id)initWithCoder:(NSCoder *)coder;

How do I apply this method to the byte stream that I have received to 
instantiate a populated NSDictionary object?

-Carl


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/jwdixon%40gmail.com

This email sent to jwdi...@gmail.com
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com