bwJSONMatcher is a lightweight Objective-C library that simplifies JSON validation and parsing. It provides an intuitive way to define matchers for JSON data, making it easy to validate and extract desired values from complex JSON structures.
Installation
- Open your terminal and navigate to your project directory.
- Run the following command to integrate bwJSONMatcher with CocoaPods:
pod 'bwJSONMatcher'
- Once the installation is complete, open your project workspace.
- Import the bwJSONMatcher module in your source files:
#import <bwJSONMatcher/bwJSONMatcher.h>
Getting Started
Follow the steps below to get started with bwJSONMatcher:
- Create a new instance of bwJSONMatcher:
bwJSONMatcher *jsonMatcher = [[bwJSONMatcher alloc] init];
- Define your JSON matcher using the available matchers provided by bwJSONMatcher. You can chain multiple matchers together for complex validation:
NSDictionary *matcher = [jsonMatcher createMatcher:^(id<bwJSONMatcherProtocol> jsonMatcher) {
[jsonMatcher withStringKey:@"name" valueMustMatch:^(id<bwStringMatcherProtocol> matcher) {
[matcher shouldEqual:@"John Doe"];
[matcher shouldNotEqual:@"Jane Smith"];
}];
[jsonMatcher withDictionaryKey:@"address" valueMustMatch:^(id<bwDictionaryMatcherProtocol> matcher) {
[matcher withStringKey:@"street" valueMustMatch:^(id<bwStringMatcherProtocol> streetMatcher) {
[streetMatcher shouldContain:@"Main Street"];
}];
[matcher withNumberKey:@"zipcode" valueMustMatch:^(id<bwNumberMatcherProtocol> zipcodeMatcher) {
[zipcodeMatcher shouldEqual:12345];
}];
}];
}];
- Parse your JSON data and validate it against the defined matcher:
NSData *jsonData = ... // Your JSON data
NSError *error;
id parsedData = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
if ([jsonMatcher validateMatcher:matcher againstData:parsedData]) {
// JSON data matches the defined matcher
// Extract required values using the provided methods
NSString *name = [jsonMatcher stringValueForKey:@"name" fromData:parsedData];
NSDictionary *address = [jsonMatcher dictionaryValueForKey:@"address" fromData:parsedData];
} else {
// JSON data does not match the defined matcher
// Handle validation failure
NSLog(@"JSON validation failed: %@", error.localizedDescription);
}
Available Matchers
bwJSONMatcher provides various matchers to validate different types of JSON data. Below are the available matchers:
bwStringMatcher
The bwStringMatcher validates JSON string values. You can use the following methods to define match conditions:
shouldEqual:
– Match if the string is equal to the provided value.shouldNotEqual:
– Match if the string is not equal to the provided value.shouldContain:
– Match if the string contains the provided substring.shouldNotContain:
– Match if the string does not contain the provided substring.shouldMatchRegexPattern:
– Match if the string matches the provided regular expression pattern.
bwNumberMatcher
The bwNumberMatcher validates JSON number values. You can use the following methods to define match conditions:
shouldEqual:
– Match if the number is equal to the provided value.shouldNotEqual:
– Match if the number is not equal to the provided value.
bwDictionaryMatcher
The bwDictionaryMatcher validates JSON dictionary values. You can use the following methods to define match conditions:
withStringKey:valueMustMatch:
– Match if the dictionary has a string key-value pair that matches the provided matcher.withNumberKey:valueMustMatch:
– Match if the dictionary has a number key-value pair that matches the provided matcher.withDictionaryKey:valueMustMatch:
– Match if the dictionary has a dictionary key-value pair that matches the provided matcher.
bwArrayMatcher
The bwArrayMatcher validates JSON array values. You can use the following methods to define match conditions:
withElementMatcher:
– Match if the array contains an element that matches the provided matcher.withExactElements:inOrder:
– Match if the array has exactly the specified elements (in order).
Conclusion
bwJSONMatcher is a powerful tool for validating and parsing JSON data in Objective-C projects. It simplifies the process by providing an intuitive way to define matchers for JSON structures. With bwJSONMatcher, you can validate complex JSON data and extract required values with ease.