@@ -19,7 +19,6 @@ - (id)init
19
19
yaml_emitter_initialize (&emitter);
20
20
21
21
buffer = [NSMutableData data ];
22
- // I am overjoyed that this works.
23
22
// Coincidentally, the order of arguments to CFDataAppendBytes are just right
24
23
// such that if I pass the buffer as the data parameter, I can just use
25
24
// a pointer to CFDataAppendBytes to tell the emitter to write to the NSMutableData.
@@ -34,10 +33,10 @@ - (void)emitItem:(id)item
34
33
// Create and initialize a document to hold this.
35
34
yaml_document_t document;
36
35
memset (&document, 0 , sizeof (document));
37
- // The double usage of !usesExplicitDelimiters is to indicate that both the start and the end of the
38
- // document should be delimited, if at all .
36
+ // The double usage of !usesExplicitDelimiters corresponds to explicitly
37
+ // delimiting the start and end of various documents .
39
38
yaml_document_initialize (&document, NULL , NULL , NULL , !usesExplicitDelimiters, !usesExplicitDelimiters);
40
-
39
+ // TODO: Make this into a proper private method.
41
40
[self _writeItem: item toDocument: &document];
42
41
yaml_emitter_dump (&emitter, &document);
43
42
yaml_document_delete (&document);
@@ -46,23 +45,27 @@ - (void)emitItem:(id)item
46
45
- (int )_writeItem : (id )item toDocument : (yaml_document_t *)doc ;
47
46
{
48
47
int nodeID = 0 ;
49
- if ([item isKindOfClass: [NSArray class ]] || [item isKindOfClass: [NSSet class ]]) {
50
- // emit beginning sequence
51
- nodeID = yaml_document_add_sequence (doc, (yaml_char_t *)YAML_DEFAULT_SEQUENCE_TAG, YAML_ANY_SEQUENCE_STYLE);
52
- for (id subitem in item) {
53
- int newItem = [self _writeItem: subitem toDocument: doc];
54
- yaml_document_append_sequence_item (doc, nodeID, newItem);
55
- }
56
- } else if ([item isKindOfClass: [NSDictionary class ]]) {
57
- // emit beginning mapping
48
+ // #keyEnumerator covers NSMapTable/NSHashTable/NSDictionary
49
+ if ([item respondsToSelector: @selector (keyEnumerator )]) {
50
+ // Add a mapping node.
58
51
nodeID = yaml_document_add_mapping (doc, (yaml_char_t *)YAML_DEFAULT_MAPPING_TAG, YAML_ANY_MAPPING_STYLE);
59
52
for (id key in item) {
60
53
int keyID = [self _writeItem: key toDocument: doc];
61
54
int valueID = [self _writeItem: [item objectForKey: key] toDocument: doc];
62
55
yaml_document_append_mapping_pair (doc, nodeID, keyID, valueID);
63
56
}
57
+ // #objectEnumerator covers NSSet/NSArray.
58
+ } else if ([item respondsToSelector: @selector (objectEnumerator )]) {
59
+ // emit beginning sequence
60
+ nodeID = yaml_document_add_sequence (doc, (yaml_char_t *)YAML_DEFAULT_SEQUENCE_TAG, YAML_ANY_SEQUENCE_STYLE);
61
+ for (id subitem in item) {
62
+ int newItem = [self _writeItem: subitem toDocument: doc];
63
+ yaml_document_append_sequence_item (doc, nodeID, newItem);
64
+ }
65
+ // Everything else is a scalar.
64
66
} else {
65
67
// TODO: Add optional support for tagging emitted items.
68
+ // TODO: Wrap long lines.
66
69
nodeID = yaml_document_add_scalar (doc, (yaml_char_t *)YAML_DEFAULT_SCALAR_TAG, (yaml_char_t *)[[item description ] UTF8String ], strlen ([[item description ] UTF8String ]), YAML_ANY_SCALAR_STYLE);
67
70
}
68
71
return nodeID;
0 commit comments