Code Monkey home page Code Monkey logo

swiftpath's Introduction

SwiftPath

Build Status Swift 4.2-5.3 compatible swift-package-manager CocoaPods compatible License: MIT

JSONPath for Swift

Problem

Somtimes, you don't want to hard-code the mapping from JSON to your model. You may want a generic way to model data from different sources. You may want to be able to update your remote data structure without having to update your app binary.

Solution

SwiftPath allows you to keep your data source and your data mapping dynamic.

Installation

SwiftPath is available through CocoaPods and Carthage.

CocoaPods

pod "SwiftPath"

Carthage

github "g-mark/SwiftPath" "master"

Documentation

SwiftPath is a Swift implementation of JSONPath, which allows you to extract a subset of data from a larger chunk of JSON.

Sipmle usage

Wildcards

You can add a wildcard to objects to convert the current object to an array of it's values:

// Given:
{
  "books" : {
    "one" : { "title": "one" },
    "two" : { "title": "two" },
    "three" : { "title": "three" }
  }
}

// When using this path:
$.books.*

// Then you'll get this output:
[ { "title": "one" }, { "title": "two" }, { "title": "three" } ]

Mapping properties

This is a difference from the JSONPath 'spec'. With SwiftPath, you can re-map property names to match your model in code. When collecting a subset of properties, you can do things like this (renamning the value property to id):

// Given:
[
  {"name": "one", "value": 1, "extra": true },
  {"name": "one", "value": 2, "extra": true },
  {"name": "one", "value": 3, "extra": true }
]

// When using this path:
$['name', 'value'=>'id']

// Then you'll get this output:
[
  {"name": "one", "id": 1 },
  {"name": "one", "id": 2 },
  {"name": "one", "id": 3 }
]

Publishing

To publish a new version of SwiftPath:

  1. Update version number in SwiftPath.podspec
  2. Create a release branch off of production with version: e.g., release/#.#.#
  3. Merge develop into release/version branch
  4. Create PR into production from release/version branch
  5. Once CI passess, tag it with #.#.#, merge into production, and delete the release/version branch
  6. Run pod trunk push SwiftLint.podspec

swiftpath's People

Contributors

g-mark avatar dwaynecoussement avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.