Thanks to visit codestin.com
Credit goes to github.com

Skip to content

erisean/SwiftyJSearch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwiftyJSearch

SwiftyJSearch extends the functionality of SwiftyJSON with JSON search functionality.

  1. SwiftyJSON
  2. Requirements
  3. Integration
  4. Usage
  5. Metadata

SwiftyJSON

This package extends the use of SwiftyJSON, a JSON handling package for Swift.

Checkout the project here: SwiftyJSON

Changes made to the SwiftyJSON source code are listed in SwiftyJSON Changes

Requirements

  • iOS 13.0+, macOS 10.13+
  • Xcode 14

Integration

Swift Package Manager

You can use The Swift Package Manager to install SwiftyJSearch by adding the proper description to your Package.swift file:

// swift-tools-version:5.8
import PackageDescription

let package = Package(
    name: "YOUR_PROJECT_NAME",
    dependencies: [
        .package(url: "https://github.com/seanericksonpl12/SwiftyJSearch.git", from: "1.0.0"),
    ]
)

Then run swift build whenever you get prepared.

CocoaPods

You can use CocoaPods to install SwiftyJSearch by adding it to your Podfile:

platform :ios, '13.0'
use_frameworks!

target 'MyApp' do
    pod 'SwiftyJSON', :git => 'https://github.com/seanericksonpl12/SwiftyJSearch.git'
end

Usage

SwiftyJSON Extensions

Initialization

import SwiftyJSearch
let json = JSON(data: someData)

For all JSON object usage info, check out the SwiftyJSON Docs

Tree Format

Large JSON data can be difficult to read, especially when deeply nested. SwiftyJSearch makes reading JSON much easier with the treeFormat variable.

Example JSON:

[
    {
        "car types": [
            {
                "brand": "Chevy",
                "gasoline": true,
                "models": [
                    "Camaro",
                    "Corvette",
                    "Silverado",
                    "Suburban"
                ]
            },
            {
                "my own car": {
                    "miles": 91242,
                    "gasoline": true,
                    "parts" : [
                        "steering wheel",
                        "brake pads",
                        "chassis"
                    ]
                }
            }
        ]
    }
]
let json = JSON(data: jsonDataFromAbove)
print(json.treeFormat)

// output:

└── car types
        ├── #
           ├── models
                  ├── Camaro
                  ├── Corvette
                  ├── Silverado
                  └── Suburban
           ├── gasoline
              └── true
           └── brand
               └── Chevy
        └── my own car
            └── #
                ├── miles
                   └── 91242
                ├── gasoline
                   └── true
                └── parts
                        ├── steering wheel
                        ├── brake pads
                        └── chassis

To increase readability, '#' nodes represent dictionaries with more than a single key

Since JSON Dictionaries are unordered, the order of pairs in the dictionary will vary

Breadth First Search

let json = JSON(data: jsonDataFromAbove)
let values = json.bfs(for: "my own car")
print(values.first!.treeFormat)

// output:

#
├── parts
       ├── steering wheel
       ├── brake pads
       └── chassis
├── miles
   └── 91242
└── gasoline
    └── true

Or search multiple keys with one search:

let json = JSON(data: jsonDataFromAbove)
let valueDictionary = json.bfs(for: ["models", "parts"])

if let parts = valueDictionary["parts"]?.first, let models = valueDictionary["models"]?.first {
    print(models.treeFormat)
    print(parts.treeFormat)
}

// output:

├── Camaro
├── Corvette
├── Silverado
└── Suburban

├── steering wheel
├── brake pads
└── chassis

Depth First Search

Works the same as above, only running the search depth - first.

let json = JSON(data: jsonDataFromAbove)
let values = json.dfs(for: "my own car")
print(values.first!.treeFormat)

// output:

#
├── parts
       ├── steering wheel
       ├── brake pads
       └── chassis
├── miles
   └── 91242
└── gasoline
    └── true

Path

Print out the direct path through the json to a specific key or value.

let json = JSON(data: jsonDataFromAbove)
let path = json.path(to: "steering wheel")
print(path)

// output:
[...] -> car types : [...] -> my own car : parts : [...] -> steering wheel

JSONTree

For an alternate way to store and manipulate JSON Data, you can use the JSONTree structure. JSONTree is a basic tree data structure, but built around storing JSON data.

Initialization

let json = JSON(data: jsonDataFromAbove)
let tree = JSONTree(json: json)

Or build it yourself:

let root = JSONTree.Node(children: [], content: .string("root"))
let tree = JSONTree(root: root)

Content Types

JSONTree nodes store values with the ContentType Enum.

ContentType values:
    .string(String)
    .number(NSNumber)
    .bool(Bool)
    .null

Pretty Format

The JSONTree prettyFormat works the same as the JSON treeFormat:

let json = JSON(data: jsonDataFromAbove)
let tree = JSONTree(json: json)
print(tree.prettyFormat)

// output:

└── car types
        ├── #
           ├── models
                  ├── Camaro
                  ├── Corvette
                  ├── Silverado
                  └── Suburban
           ├── gasoline
              └── true
           └── brand
               └── Chevy
        └── my own car
            └── #
                ├── miles
                   └── 91242
                ├── gasoline
                   └── true
                └── parts
                        ├── steering wheel
                        ├── brake pads
                        └── chassis

JSONTree is autobalanced by sorting unordered dictionaries on init to improve search speed, so output between json.treeFormat and tree.prettyFormat may vary

Search

Search for any data in the tree, similar to bfs with json objects, and get a reference to the tree node containing it:

let json = JSON(data: jsonDataFromAbove)
let tree = JSONTree(json: json)

let node = tree.search(for: "chassis")
node?.content = .string("radio")
print(tree.prettyFormat)

// output:
...
    └── my own car
        └── #
            ├── miles
               └── 91242
            ├── gasoline
               └── true
            └── parts
                    ├── steering wheel
                    ├── brake pads
                    └── radio

Modify

Modify the json yourself by adding new children to nodes, or removing nodes with the provided functions:

let json = JSON(data: jsonDataFromAbove)
let tree = JSONTree(json: json)
tree.removeAll(where: { $0.content == .string("parts") || $0.content == .number(91242)})
print(tree.prettyFormat)

// output:

...
    └── my own car
        └── #
            ├── miles
            ├── gasoline
               └── true
            ├── steering wheel
            ├── brake pads
            └── chassis

SwiftyJSON Changes

Due to a lack of updates in the SwiftyJSON project in recent years, this package contains the source code from SwiftyJSON with updated iOS/MacOS version support rather than linking it as a dependency. All changes made to the original SwiftyJSON code are listed below.

All other functionality not listed below remain accurate to the original projects README.

Int8

Int8 appears to no longer be a valid value for constructing an NSNumber (see NSNumber). As such, int8 and int8Value have been removed as JSON variables. uInt8 is still valid and available.

Metadata

SwiftyJSON - SwiftyJSON SwiftyJSON Authors - lingoer, wongzigii, Luke Tang

SwiftyJSearch Author - Sean Erickson [email protected]

About

extend the functionality of SwiftyJSON with JSON search functionality and better formatting

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages