Release: v0.8.0

Published

This release adds new JSON functions to Phel.

JSON

You can use the phel\json namespace to encode or decode a JSON directly from Phel.

Encode

You can encode a Phel data structure to JSON using the encode function.

(ns my-namespace
  (:require phel\json))

(json/encode [1 2 3]) # "[1,2,3]"
(json/encode {:key1 1 :key2 "value2"} # "{\"key1\":1,\"key2\":\"value2\"}"

Decode

You can decode a JSON string to a Phel data structure using the decode function.

Considering your.json as:

{
  "name": "John Doe",
  "age": 30,
  "image": "",
  "email": "john@example.com",
  "phone": "(912) 555-4321",
  "url": "https://example.com",
  "location": {
    "address": "2712 Broadway St",
    "postalCode": "CA 94115",
    "countryCode": "US"
  }
}
(ns my-namespace
  (:require phel\json))

(def your-json (php/file_get_contents (str __DIR__ "/your.json")))

(json/decode your-json)
# Evaluates to
# {
#   :name "John Doe"
#   :age 30
#   :image ""
#   :email "john@example.com"
#   :phone "(912) 555-4321"
#   :url "https://example.com"
#   :location {
#     :address "2712 Broadway St"
#     :postalCode "CA 94115"
#     :countryCode "US"
#   }
# }

Other improvements

  • Allow strings on empty?
  • Improved error message when using strings on count
  • Added contains-value? function

Full list

For a full list of changes have a look at the Changelog.