Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged support for parsing boolean, number, date and null + some cleanup #17

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions YAML-Framework.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Pod::Spec.new do |s|
s.name = "YAML-Framework"
s.version = "0.0.2"
s.summary = "Proper YAML support for Objective-C. Based on recommended LibYAML."
s.description = "YAML.framework provides support for YAML (de)serialisation similarly to standard NSPropertyListSerialization. Based on C LibYAML library by Kirill Simonov."
s.homepage = "https://github.com/mirek/YAML.framework"
s.license = {
:type => 'MIT',
:text => <<-LICENSE
Copyright (c) 2010 Mirek Rusin (YAML.framework)
2006 Kirill Simonov (LibYAML)

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
LICENSE
}

s.authors = { "Mirek Rusin" => "[email protected]", "Kirill Simonov" => "kiril" }
s.source = { :git => "https://github.com/mirek/YAML.framework.git", :tag => 'v0.0.2' }
s.source_files = 'YAMLSerialization.{h,m}', 'yaml-0.1.4/config.h'
s.public_header_files = 'YAMLSerialization.h'
s.requires_arc = false
s.dependency 'LibYAML', '~> 0.1.4'
end
38 changes: 20 additions & 18 deletions YAML.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
D319EEB413E5EE2A000EB46D /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = D319EEB213E5EE2A000EB46D /* InfoPlist.strings */; };
D319EEB713E5EE2A000EB46D /* YAMLUnitTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D319EEB613E5EE2A000EB46D /* YAMLUnitTests.m */; };
D319EEBD13E5EFA5000EB46D /* YAMLSerialization.m in Sources */ = {isa = PBXBuildFile; fileRef = EBF83D8A124D7A3E008517E5 /* YAMLSerialization.m */; };
D319EEBE13E5F093000EB46D /* (null) in Sources */ = {isa = PBXBuildFile; };
D319EEBF13E5F093000EB46D /* (null) in Sources */ = {isa = PBXBuildFile; };
D319EEC013E5F093000EB46D /* (null) in Sources */ = {isa = PBXBuildFile; };
D319EEC113E5F093000EB46D /* (null) in Sources */ = {isa = PBXBuildFile; };
D319EEC213E5F093000EB46D /* (null) in Sources */ = {isa = PBXBuildFile; };
D319EEC313E5F093000EB46D /* (null) in Sources */ = {isa = PBXBuildFile; };
D319EEC413E5F093000EB46D /* (null) in Sources */ = {isa = PBXBuildFile; };
D319EEC513E5F093000EB46D /* (null) in Sources */ = {isa = PBXBuildFile; };
D3E92E6413E5F68C00BA5B63 /* basic.yaml in Resources */ = {isa = PBXBuildFile; fileRef = D3E92E6313E5F68C00BA5B63 /* basic.yaml */; };
D58802A818AC5B1800D5E01A /* api.c in Sources */ = {isa = PBXBuildFile; fileRef = EB8C1AB114B795E200BCDC1A /* api.c */; };
D58802A918AC5B1800D5E01A /* dumper.c in Sources */ = {isa = PBXBuildFile; fileRef = EB8C1AB214B795E200BCDC1A /* dumper.c */; };
D58802AA18AC5B1800D5E01A /* emitter.c in Sources */ = {isa = PBXBuildFile; fileRef = EB8C1AB314B795E200BCDC1A /* emitter.c */; };
D58802AB18AC5B1800D5E01A /* loader.c in Sources */ = {isa = PBXBuildFile; fileRef = EB8C1AB414B795E200BCDC1A /* loader.c */; };
D58802AC18AC5B1800D5E01A /* parser.c in Sources */ = {isa = PBXBuildFile; fileRef = EB8C1AB514B795E200BCDC1A /* parser.c */; };
D58802AD18AC5B1800D5E01A /* reader.c in Sources */ = {isa = PBXBuildFile; fileRef = EB8C1AB614B795E200BCDC1A /* reader.c */; };
D58802AE18AC5B1800D5E01A /* scanner.c in Sources */ = {isa = PBXBuildFile; fileRef = EB8C1AB714B795E200BCDC1A /* scanner.c */; };
D58802AF18AC5B1800D5E01A /* writer.c in Sources */ = {isa = PBXBuildFile; fileRef = EB8C1AB814B795E200BCDC1A /* writer.c */; };
EB2B592E124E2117001FFEEB /* events.c in Sources */ = {isa = PBXBuildFile; fileRef = EB2B592D124E2117001FFEEB /* events.c */; };
EB2B5952124E230A001FFEEB /* yaml in Copy YAML Files */ = {isa = PBXBuildFile; fileRef = EB6C424F124D9B3E00886AD1 /* yaml */; };
EB6C3FBA124D872600886AD1 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EB6C3FB9124D872600886AD1 /* Foundation.framework */; };
Expand Down Expand Up @@ -508,16 +508,16 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D58802A818AC5B1800D5E01A /* api.c in Sources */,
D58802A918AC5B1800D5E01A /* dumper.c in Sources */,
D58802AA18AC5B1800D5E01A /* emitter.c in Sources */,
D58802AB18AC5B1800D5E01A /* loader.c in Sources */,
D58802AC18AC5B1800D5E01A /* parser.c in Sources */,
D58802AD18AC5B1800D5E01A /* reader.c in Sources */,
D58802AE18AC5B1800D5E01A /* scanner.c in Sources */,
D58802AF18AC5B1800D5E01A /* writer.c in Sources */,
D319EEB713E5EE2A000EB46D /* YAMLUnitTests.m in Sources */,
D319EEBD13E5EFA5000EB46D /* YAMLSerialization.m in Sources */,
D319EEBE13E5F093000EB46D /* (null) in Sources */,
D319EEBF13E5F093000EB46D /* (null) in Sources */,
D319EEC013E5F093000EB46D /* (null) in Sources */,
D319EEC113E5F093000EB46D /* (null) in Sources */,
D319EEC213E5F093000EB46D /* (null) in Sources */,
D319EEC313E5F093000EB46D /* (null) in Sources */,
D319EEC413E5F093000EB46D /* (null) in Sources */,
D319EEC513E5F093000EB46D /* (null) in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -654,7 +654,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_ENABLE_OBJC_ARC = NO;
COMBINE_HIDPI_IMAGES = YES;
COPY_PHASE_STRIP = NO;
FRAMEWORK_SEARCH_PATHS = "$(DEVELOPER_LIBRARY_DIR)/Frameworks";
Expand All @@ -673,6 +673,7 @@
INFOPLIST_FILE = "YAMLUnitTests/YAMLUnitTests-Info.plist";
MACOSX_DEPLOYMENT_TARGET = 10.7;
ONLY_ACTIVE_ARCH = NO;
OTHER_CFLAGS = "-DHAVE_CONFIG_H";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
WRAPPER_EXTENSION = octest;
Expand All @@ -683,7 +684,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_ENABLE_OBJC_ARC = NO;
COMBINE_HIDPI_IMAGES = YES;
COPY_PHASE_STRIP = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
Expand All @@ -696,6 +697,7 @@
GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES;
INFOPLIST_FILE = "YAMLUnitTests/YAMLUnitTests-Info.plist";
MACOSX_DEPLOYMENT_TARGET = 10.7;
OTHER_CFLAGS = "-DHAVE_CONFIG_H";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
WRAPPER_EXTENSION = octest;
Expand Down
4 changes: 2 additions & 2 deletions YAMLSerialization.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//
// YAMLSerialization.h
// YAML Serialization support by Mirek Rusin based on C library LibYAML by Kirill Simonov
// Released under MIT License
// Released under MIT License
//
// Copyright 2010 Mirek Rusin
// Copyright 2010 Stanislav Yudin
// Copyright 2010 Stanislav Yudin
//

#import <Foundation/Foundation.h>
Expand Down
118 changes: 75 additions & 43 deletions YAMLSerialization.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// YAMLSerialization.m
// YAML Serialization support by Mirek Rusin based on C library LibYAML by Kirill Simonov
// Released under MIT License
// Released under MIT License
//
// Copyright 2010 Mirek Rusin
// Copyright 2010 Stanislav Yudin
Expand All @@ -13,28 +13,63 @@

// Assumes NSError **error is in the current scope
#define YAML_SET_ERROR(errorCode, description, recovery) \
if (error) \
*error = [NSError errorWithDomain: YAMLErrorDomain \
code: errorCode \
userInfo: [NSDictionary dictionaryWithObjectsAndKeys: \
description, NSLocalizedDescriptionKey, \
recovery, NSLocalizedRecoverySuggestionErrorKey, \
nil]]
if (error) \
*error = [NSError errorWithDomain: YAMLErrorDomain \
code: errorCode \
userInfo: [NSDictionary dictionaryWithObjectsAndKeys: \
description, NSLocalizedDescriptionKey, \
recovery, NSLocalizedRecoverySuggestionErrorKey, \
nil]]

@implementation YAMLSerialization

static NSNumber *ParseBoolean(NSString *str) {
for (NSString *s in @[@"y", @"yes", @"true", @"on"])
if ([s isEqualToString:str.lowercaseString])
return @YES;
for (NSString *s in @[@"n", @"no", @"false", @"off"])
if ([s isEqualToString:str.lowercaseString])
return @NO;
return nil;
}

static NSNumber *ParseNumber(NSString *str) {
static dispatch_once_t numberFormatterOnceToken;
static NSNumberFormatter *numberFormatter = nil;
dispatch_once(&numberFormatterOnceToken, ^{
numberFormatter = [[NSNumberFormatter alloc] init];
});
return [[numberFormatter numberFromString:str] retain];
}

static NSDate *ParseDate(NSString *str) {
static dispatch_once_t dateFormatterOnceToken;
static NSDateFormatter *dateFormatter = nil;
dispatch_once(&dateFormatterOnceToken, ^{
dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd";
});
return [[dateFormatter dateFromString:str] retain];
}

static NSNull *ParseNull(NSString *str) {
if (str.length == 0 || [str isEqualToString:@"~"] || [str.lowercaseString isEqualToString:@"null"])
return [NSNull null];
return nil;
}

#pragma mark Reading YAML

static int
__YAMLSerializationParserInputReadHandler (void *data, unsigned char *buffer, size_t size, size_t *size_read) {
NSInteger outcome = [(NSInputStream *) data read: (uint8_t *) buffer maxLength: size];
if (outcome < 0) {
*size_read = 0;
return NO;
} else {
*size_read = outcome;
return YES;
}
if (outcome < 0) {
*size_read = 0;
return NO;
} else {
*size_read = outcome;
return YES;
}
}

// Serialize single, parsed document. Does not destroy the document.
Expand All @@ -56,13 +91,6 @@ @implementation YAMLSerialization
}
}

if (opt & kYAMLReadOptionStringScalars) {
// Supported
} else {
YAML_SET_ERROR(kYAMLErrorInvalidOptions, @"Currently only kYAMLReadOptionStringScalars is supported", @"Serialize with kYAMLReadOptionStringScalars option");
return nil;
}

yaml_node_t *node = NULL;
yaml_node_item_t *item = NULL;
yaml_node_pair_t *pair = NULL;
Expand All @@ -78,23 +106,27 @@ @implementation YAMLSerialization
// Create all objects, don't fill containers yet...
for (node = document->nodes.start, i = 0; node < document->nodes.top; node++, i++) {
switch (node->type) {
case YAML_SCALAR_NODE:
objects[i] = [[stringClass alloc] initWithUTF8String: (const char *)node->data.scalar.value];
case YAML_SCALAR_NODE: {
id value = [[stringClass alloc] initWithUTF8String: (const char *)node->data.scalar.value];
if (!(opt & kYAMLReadOptionStringScalars)) {
value = ParseNull(value) ?: ParseBoolean(value) ?: ParseNumber(value) ?: ParseDate(value) ?: value;
}
objects[i] = value;
if (!root) root = objects[i];
break;

}
case YAML_SEQUENCE_NODE:
objects[i] = [[arrayClass alloc] initWithCapacity: node->data.sequence.items.top - node->data.sequence.items.start];
if (!root) root = objects[i];
break;
objects[i] = [[arrayClass alloc] initWithCapacity: node->data.sequence.items.top - node->data.sequence.items.start];
if (!root) root = objects[i];
break;

case YAML_MAPPING_NODE:
objects[i] = [[dictionaryClass alloc] initWithCapacity: node->data.mapping.pairs.top - node->data.mapping.pairs.start];
if (!root) root = objects[i];
break;
objects[i] = [[dictionaryClass alloc] initWithCapacity: node->data.mapping.pairs.top - node->data.mapping.pairs.start];
if (!root) root = objects[i];
break;

default:
break;
break;
}
}

Expand All @@ -110,10 +142,10 @@ @implementation YAMLSerialization
for (pair = node->data.mapping.pairs.start; pair < node->data.mapping.pairs.top; pair++)
[objects[i] setObject: objects[pair->value - 1]
forKey: objects[pair->key - 1]];
break;
break;

default:
break;
break;
}
}

Expand All @@ -136,8 +168,8 @@ @implementation YAMLSerialization
}

+ (NSMutableArray *) objectsWithYAMLStream: (NSInputStream *) stream
options: (YAMLReadOptions) opt
error: (NSError **) error
options: (YAMLReadOptions) opt
error: (NSError **) error
{
NSMutableArray *documents = [NSMutableArray array];
id documentObject = nil;
Expand Down Expand Up @@ -190,8 +222,8 @@ + (id) objectWithYAMLStream: (NSInputStream *) stream options: (YAMLReadOptions)
}

+ (NSMutableArray *) objectsWithYAMLData: (NSData *) data
options: (YAMLReadOptions) opt
error: (NSError **) error;
options: (YAMLReadOptions) opt
error: (NSError **) error;
{
NSMutableArray *result = nil;
if (data != nil) {
Expand Down Expand Up @@ -236,23 +268,23 @@ + (id) objectWithYAMLString: (NSString *) string options: (YAMLReadOptions) opt
int valueIndex = __YAMLSerializationAddObject(document, [value objectForKey: key]);
yaml_document_append_mapping_pair(document, result, keyIndex, valueIndex);
}
}
}
else if ([value isKindOfClass: [NSArray class]]) {
result = yaml_document_add_sequence(document, NULL, YAML_BLOCK_SEQUENCE_STYLE);
for (id element in value) {
int elementIndex = __YAMLSerializationAddObject(document, element);
yaml_document_append_sequence_item(document, result, elementIndex);
}
}
else {
else {
NSString *string = nil;
if ([value isKindOfClass: [NSString class]]) {
string = value;
} else {
string = [value stringValue];
}
result = yaml_document_add_scalar(document, NULL, (yaml_char_t *)[string UTF8String], (int) [string length], YAML_PLAIN_SCALAR_STYLE);
}
}
return (int) result;
}

Expand All @@ -275,8 +307,8 @@ + (BOOL) __YAMLSerializationAddRootObjectAndEmit: (id) object emitter: (yaml_emi

+ (BOOL) writeObject: (id) object
toYAMLStream: (NSOutputStream *) stream
options: (YAMLWriteOptions) opt
error: (NSError **) error
options: (YAMLWriteOptions) opt
error: (NSError **) error
{
BOOL result = YES;
yaml_emitter_t emitter;
Expand Down
16 changes: 8 additions & 8 deletions YAMLUnitTests/YAMLUnitTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ - (void)tearDown
- (void)testReadData
{
NSString *fileName = [[NSBundle bundleForClass:[self class]] pathForResource:@"basic" ofType:@"yaml"];
NSData *data = [NSData dataWithContentsOfFile:fileName];
NSData *data = [NSData dataWithContentsOfFile:fileName];
NSTimeInterval before = [[NSDate date] timeIntervalSince1970];
NSMutableArray *yaml = [YAMLSerialization YAMLWithData: data options: kYAMLReadOptionStringScalars error: nil];
NSLog(@"YAMLWithData took %f", ([[NSDate date] timeIntervalSince1970] - before));
NSLog(@"%@", yaml);
NSMutableArray *yaml = [YAMLSerialization YAMLWithData: data options: kYAMLReadOptionStringScalars error: nil];
NSLog(@"YAMLWithData took %f", ([[NSDate date] timeIntervalSince1970] - before));
NSLog(@"%@", yaml);
STAssertEquals((int) 10, (int) [yaml count], @"Wrong number of expected objects");

}
Expand All @@ -42,10 +42,10 @@ - (void)testReadStream
NSString *fileName = [[NSBundle bundleForClass:[self class]] pathForResource:@"basic" ofType:@"yaml"];
NSInputStream *stream = [[NSInputStream alloc] initWithFileAtPath: fileName];
NSError *err = nil;
NSTimeInterval before2 = [[NSDate date] timeIntervalSince1970];
NSMutableArray *yaml2 = [YAMLSerialization YAMLWithStream: stream options: kYAMLReadOptionStringScalars error: &err];
NSLog(@"YAMLWithStream took %f", ([[NSDate date] timeIntervalSince1970] - before2));
NSLog(@"%@", yaml2);
NSTimeInterval before2 = [[NSDate date] timeIntervalSince1970];
NSMutableArray *yaml2 = [YAMLSerialization YAMLWithStream: stream options: kYAMLReadOptionStringScalars error: &err];
NSLog(@"YAMLWithStream took %f", ([[NSDate date] timeIntervalSince1970] - before2));
NSLog(@"%@", yaml2);
STAssertEquals((int) 10, (int) [yaml2 count], @"Wrong number of expected objects");

}
Expand Down
Loading