<commit_before># A JSON parser with concise API written in Swift for iOS, macOS, tvOS and watchOS

[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)][carthage]
[![CocoaPods Version](https://img.shields.io/cocoapods/v/JsonSwiftson.svg?style=flat)][cocoadocs]
[![License](https://img.shields.io/cocoapods/l/JsonSwiftson.svg?style=flat)][cocoadocs]
[![Platform](https://img.shields.io/cocoapods/p/JsonSwiftson.svg?style=flat)][cocoadocs]
[cocoadocs]: http://cocoadocs.org/docsets/JsonSwiftson
[carthage]: https://github.com/Carthage/Carthage

JsonSwiftson is a JSON parser that exposes just a single API method `map()` for mapping JSON attributes to Swift types.

<img src='https://raw.githubusercontent.com/evgenyneu/JsonSwiftson/master/Graphics/GithubLogo/json_swiftson_parser.png' width='256' alt='JsonSwiftson JSON parser for Swift'>

### The features of this project

* Maps JSON text into Swift structures or classes.
* Keeps the library [source code](https://github.com/evgenyneu/JsonSwiftson/blob/master/JsonSwiftson/JsonSwiftson.swift) tiny by using Swift generics and taking advantage of type casting features of the language.
* Supports casting to optional types.
* Indicates if the mapping was successful.
* Makes sure the mapping is fast.

### Example

The following is an example of serializing JSON text into a `Person` structure with JsonSwiftson library.

```Swift
struct Person {
  let name: String
  let age: Int
}

let j = JsonSwiftson(json: "{ \"name\": \"Peter\", \"age\": 41 }")

let person = Person(
  name: j["name"].map() ?? "",
  age: j["age"].map() ?? 0
)

if !j.ok { /* report error */ }
```

## Setup

There are three ways you can add JsonSwiftson into your project.

#### Add the source file (iOS 7+)

Simply add [JsonSwiftson.swift](https://github.com/evgenyneu/JsonSwiftson/blob/master/JsonSwiftson/JsonSwiftson.swift) file to your Xcode project.

#### Setup with Carthage (iOS 8+)

Alternatively, add `github "evgenyneu/JsonSwiftson" ~> 3.0` to your Cartfile and run `carthage update`.

#### Setup with CocoaPods (iOS 8+)

If you are using CocoaPods add this text to your Podfile and run `pod install`.

    use_frameworks!
    target 'Your target name'
    pod 'JsonSwiftson', '~> 3.0'


#### Setup with Swift Package Manager

Add the following text to your Package.swift and run `swift build`.

```Swift
import PackageDescription

let package = Package(
    name: "YourPackageName",
    targets: [],
    dependencies: [
        .Package(url: "https://github.com/evgenyneu/JsonSwiftson.git",
                 versions: Version(3,0,0)..<Version(4,0,0))
    ]
)
```


#### Legacy Swift versions

Setup a [previous version](https://github.com/evgenyneu/JsonSwiftson/wiki/Legacy-Swift-versions) of the library if you use an older version of Swift.



## Usage

1) Add `import JsonSwiftson` to your source code if you used Carthage or CocoaPods setup.

2) Create an instance of `JsonSwiftson` class.

```Swift
let j = JsonSwiftson(json: "\"Hello World\"")
```

3) Call `map` or `mapArrayOfObjects` methods.


```Swift
let string: String? = stringMapper.map()
```

4) Optionally, check `ok` property to see if mapping was successful.

```Swift
if !j.ok { /* report error */ }
```

### Map simple values

Simple values are strings, numbers and booleans. You only need to use a single method `map()`. The type of the JSON value is inferred from the context.


```Swift
// String
let stringMapper = JsonSwiftson(json: "\"Hello World\"")
let string: String? = stringMapper.map()

// Integer
let intMapper = JsonSwiftson(json: "123")
let int: Int? = intMapper.map()

// Double
let doubleMapper = JsonSwiftson(json: "123.456")
let double: Double? = doubleMapper.map()

// Boolean
let boolMapper = JsonSwiftson(json: "true")<commit_msg>Usage<commit_after># A JSON parser with concise API written in Swift for iOS, macOS, tvOS and watchOS

[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)][carthage]
[![CocoaPods Version](https://img.shields.io/cocoapods/v/JsonSwiftson.svg?style=flat)][cocoadocs]
[![License](https://img.shields.io/cocoapods/l/JsonSwiftson.svg?style=flat)][cocoadocs]
[![Platform](https://img.shields.io/cocoapods/p/JsonSwiftson.svg?style=flat)][cocoadocs]
[cocoadocs]: http://cocoadocs.org/docsets/JsonSwiftson
[carthage]: https://github.com/Carthage/Carthage

JsonSwiftson is a JSON parser that exposes just a single API method `map()` for mapping JSON attributes to Swift types.

<img src='https://raw.githubusercontent.com/evgenyneu/JsonSwiftson/master/Graphics/GithubLogo/json_swiftson_parser.png' width='256' alt='JsonSwiftson JSON parser for Swift'>

### The features of this project

* Maps JSON text into Swift structures or classes.
* Keeps the library [source code](https://github.com/evgenyneu/JsonSwiftson/blob/master/JsonSwiftson/JsonSwiftson.swift) tiny by using Swift generics and taking advantage of type casting features of the language.
* Supports casting to optional types.
* Indicates if the mapping was successful.
* Makes sure the mapping is fast.

### Example

The following is an example of serializing JSON text into a `Person` structure with JsonSwiftson library.

```Swift
struct Person {
  let name: String
  let age: Int
}

let j = JsonSwiftson(json: "{ \"name\": \"Peter\", \"age\": 41 }")

let person = Person(
  name: j["name"].map() ?? "",
  age: j["age"].map() ?? 0
)

if !j.ok { /* report error */ }
```

## Setup

There are three ways you can add JsonSwiftson into your project.

#### Add the source file (iOS 7+)

Simply add [JsonSwiftson.swift](https://github.com/evgenyneu/JsonSwiftson/blob/master/JsonSwiftson/JsonSwiftson.swift) file to your Xcode project.

#### Setup with Carthage (iOS 8+)

Alternatively, add `github "evgenyneu/JsonSwiftson" ~> 3.0` to your Cartfile and run `carthage update`.

#### Setup with CocoaPods (iOS 8+)

If you are using CocoaPods add this text to your Podfile and run `pod install`.

    use_frameworks!
    target 'Your target name'
    pod 'JsonSwiftson', '~> 3.0'


#### Setup with Swift Package Manager

Add the following text to your Package.swift and run `swift build`.

```Swift
import PackageDescription

let package = Package(
    name: "YourPackageName",
    targets: [],
    dependencies: [
        .Package(url: "https://github.com/evgenyneu/JsonSwiftson.git",
                 versions: Version(3,0,0)..<Version(4,0,0))
    ]
)
```


#### Legacy Swift versions

Setup a [previous version](https://github.com/evgenyneu/JsonSwiftson/wiki/Legacy-Swift-versions) of the library if you use an older version of Swift.



## Usage

1) Add `import JsonSwiftson` to your source code if you used Carthage or CocoaPods setup.

2) Create an instance of `JsonSwiftson` class and supply the JSON document for parsing.

```Swift
let j = JsonSwiftson(json: "\"Hello World\"")
```

3) Call `map` or `mapArrayOfObjects` methods. The type of the JSON value is inferred from the context.

```Swift
let string: String? = stringMapper.map()
```

4) Optionally, check `ok` property to see if mapping was successful.

```Swift
if !j.ok { /* report error */ }
```

### Map simple values

Simple values are strings, numbers and booleans.


```Swift
// String
let stringMapper = JsonSwiftson(json: "\"Hello World\"")
let string: String? = stringMapper.map()

// Integer
let intMapper = JsonSwiftson(json: "123")
let int: Int? = intMapper.map()

// Double
let doubleMapper = JsonSwiftson(json: "123.456")
let double: Double? = doubleMapper.map()

// Boolean
let boolMapper = JsonSwiftson(json: "true")
{'fraction_non_alphanumeric': 0.1327095413811281, 'fraction_numerical': 0.00922509225092251, 'mean_word_length': 4.432355046528275, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 20, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5463079', 'n_tokens_mistral': 2628, 'n_tokens_neox': 2429, 'n_words': 773}