All Projects → ra1028 → Alembic

ra1028 / Alembic

Licence: mit
⚗️ Functional JSON Parser - Linux Ready 🐧

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Alembic

Forge
Functional style JSON parsing in Kotlin
Stars: ✭ 106 (-7.83%)
Mutual labels:  json, parser, functional
Xml Js
Converter utility between XML text and Javascript object / JSON text.
Stars: ✭ 874 (+660%)
Mutual labels:  json, parser
Partial.lenses
Partial lenses is a comprehensive, high-performance optics library for JavaScript
Stars: ✭ 846 (+635.65%)
Mutual labels:  json, functional
Barely json
A Python parser for data that only looks like JSON
Stars: ✭ 56 (-51.3%)
Mutual labels:  json, parser
Himalaya
JavaScript HTML to JSON Parser
Stars: ✭ 758 (+559.13%)
Mutual labels:  json, parser
Dasel
Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.
Stars: ✭ 759 (+560%)
Mutual labels:  json, parser
Fast Xml Parser
Validate XML, Parse XML to JS/JSON and vise versa, or parse XML to Nimn rapidly without C/C++ based libraries and no callback
Stars: ✭ 1,021 (+787.83%)
Mutual labels:  json, parser
Jsonparser
One of the fastest alternative JSON parser for Go that does not require schema
Stars: ✭ 4,323 (+3659.13%)
Mutual labels:  json, parser
Httpz
purely functional http client with scalaz.Free
Stars: ✭ 67 (-41.74%)
Mutual labels:  monad, json
Internettools
XPath/XQuery 3.1 interpreter for Pascal with compatibility modes for XPath 2.0/XQuery 1.0/3.0, custom and JSONiq extensions, XML/HTML parsers and classes for HTTP/S requests
Stars: ✭ 82 (-28.7%)
Mutual labels:  json, parser
Go
A high-performance 100% compatible drop-in replacement of "encoding/json"
Stars: ✭ 10,248 (+8811.3%)
Mutual labels:  json, parser
Body Parser
Node.js body parsing middleware
Stars: ✭ 4,962 (+4214.78%)
Mutual labels:  json, parser
Jsonnet
Jsonnet - The data templating language
Stars: ✭ 5,257 (+4471.3%)
Mutual labels:  json, functional
Jkt
Simple helper to parse JSON based on independent schema
Stars: ✭ 22 (-80.87%)
Mutual labels:  json, parser
Crossplane
Quick and reliable way to convert NGINX configurations into JSON and back.
Stars: ✭ 407 (+253.91%)
Mutual labels:  json, parser
Parson
Lightweight JSON library written in C.
Stars: ✭ 965 (+739.13%)
Mutual labels:  json, parser
Json Rust
JSON implementation in Rust
Stars: ✭ 395 (+243.48%)
Mutual labels:  json, parser
Tomlplusplus
Header-only TOML config file parser and serializer for C++17 (and later!).
Stars: ✭ 403 (+250.43%)
Mutual labels:  json, parser
Cssparser.js
cssparser.js is a parser that generate json from css with matched orders & structures.
Stars: ✭ 61 (-46.96%)
Mutual labels:  json, parser
Java
jsoniter (json-iterator) is fast and flexible JSON parser available in Java and Go
Stars: ✭ 1,308 (+1037.39%)
Mutual labels:  json, parser

Alembic

Functional JSON Parser

Swift4 Build Status CodeBeat

CocoaPods Carthage Swift Package Manager

Platform Lincense


Feature

  • Linux Ready
  • Type-safe JSON parsing
  • Functional value transformation
  • Easy to parse nested value
  • Dependency free
  • No defined custom operators

Requirements

  • Swift4.1 or later
  • OS X 10.9 or later
  • iOS 9.0 or later
  • watchOS 2.0 or later
  • tvOS 9.0 or later
  • Linux

Installation

CocoaPods

Add the following to your Podfile:

use_frameworks!

target 'TargetName' do
  pod 'Alembic'
end

Carthage

Add the following to your Cartfile:

github "ra1028/Alembic"

Swift Package Manager

Add the following to your Package.swift:

// swift-tools-version:4.0

let package = Package(
    name: "ProjectName",
    dependencies : [
        .package(url: "https://github.com/ra1028/Alembic.git", .upToNextMajor(from: "3"))
    ]
)

Example

In example, parse the following JSON:

{
    "teams": [
        {
            "name": "Team ra1028",
            "url": "https://github.com/ra1028",
            "members": [
                {
                    "name": "Ryo Aoyama",
                    "age": 23
                },
                {
                    "name": "John Doe",
                    "age": 30
                }
            ]
        }
    ]
}

Make the JSON instance from Any, Data or String type JSON object.

// from `Any` type JSON object
let json = JSON(object)
// from JSON Data
let json = try JSON(data: data)
// from JSON String
let json = try JSON(string: string)

Parse value from JSON:

Parse the values type-safely

let memberName: String = try json.value(for: ["teams", 0, "members", 0, "name"])

Parse nullable value

let missingText: String? = try json.option(for: "missingKey")

Parse value from JSON with transforming:

Transform value using various monadic functions.

let teamUrl: URL = try json.parse(String.self, for: ["teams", 0, "url"])
        .filterMap(URL.init(string:))
        .value()

Transform nullable value if exist

let missingUrl: URL? = try json.parse(String.self, for: "missingKey")
        .filterMap(URL.init(string:))
        .option()

Mapping to model from JSON:

All types conforming to Parsable protocol and it's Array, Dictionary can be parsed.

struct Member: Parsable {
    let name: String
    let age: Int

    static func value(from json: JSON) throws -> Member {
        return try .init(
            name: json.value(for: "name"),
            age: json.value(for: "age")
        )
    }
}

extension URL: Parsable {
    public static func value(from json: JSON) throws -> URL {
        guard let url = try URL(string: json.value()) else {
            throw JSON.Error.dataCorrupted(value: json.rawValue, description: "The value was not valid url string.")
        }
        return url
    }
}

struct Team: Parsable {
    let name: String
    let url: URL
    let members: [Member]

    static func value(from json: JSON) throws -> Team {
        return try .init(
            name: json.value(for: "name"),
            url: json.value(for: "url"),
            members: json.value(for: "members")
        )
    }
}
let team: Team = try json.value(for: ["teams", 0])

Tips

The types conformed to Parsable as default.

JSON
String
Int
UInt
Double
Float
Bool
NSNumber
Int8
UInt8
Int16
UInt16
Int32
UInt32
Int64
UInt64
Decimal
Array where Element: Parsable
Dictionary where Key == String, Value: Parsable
Optional where Wrapped: Parsable

Conform to Parsable with initializer

struct Member: ParseInitializable {
    let name: String
    let age: Int

    init(with json: JSON) throws {
        name = try json.value(for: "name")
        age = try json.value(for: "age")
    }
}

Usage Reference Files

Functional operators for value transforming:

Errors

More Example

See the Test files


Playground

  1. Open Alembic.xcworkspace.
  2. Build the Alembic for Mac.
  3. Open Alembic playground in project navigator.

Contribution

Welcome to fork and submit pull requests!

Before submitting pull request, please ensure you have passed the included tests. If your pull request including new function, please write test cases for it.


License

Alembic is released under the MIT License.


Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].