Skip to content

andreabedini/make-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

make-json

A GNU Make plugin for manipulating JSON data inside Makefiles, powered by libjq.

Requirements

  • GNU Make (with loadable module support)
  • libjq-dev (or equivalent for your distribution)
  • gnumake.h (from GNU Make source or development headers)

Build

make

This compiles jv.c into the shared library jv.so.

Usage

Load the plugin with -load and call functions directly:

-load jv.so

# Build an object
obj := $(jv_object_set {},"name","make-json")
obj := $(jv_object_set $(obj),"version",1)
$(info $(obj))
# {"name":"make-json","version":1}

# Query it
$(info $(jv_object_get $(obj),"name"))
# "make-json"

# Work with arrays
arr := [1,2,3]
$(info $(jv_array_append $(arr),4))
# [1,2,3,4]

$(info $(jv_array_length $(arr)))
# 3

Arguments are JSON values separated by commas. Arguments containing commas (e.g. multi-element arrays or objects) must be stored in Make variables first, since Make uses commas as argument separators:

arr := [10,20,30]
$(info $(jv_array_get $(arr),1))
# 20

Functions

Constructors

Function Example Result
jv_null $(jv_null ) null
jv_true $(jv_true ) true
jv_false $(jv_false ) false
jv_array $(jv_array ) []
jv_object $(jv_object ) {}
jv_number $(jv_number 3.14) 3.14
jv_string $(jv_string hello) "hello"

Note: 0-argument constructors require a trailing space: $(jv_null ) not $(jv_null).

Type inspection

Function Example Result
jv_type $(jv_type 42) number
jv_type $(jv_type "hi") string

Returns one of: null, boolean, number, string, array, object.

Array

Function Args Description
jv_array_get array, index Get element at index
jv_array_set array, index, value Set element at index
jv_array_append array, value Append element
jv_array_concat array, array Concatenate two arrays
jv_array_slice array, start, end Slice from start to end
jv_array_length array Number of elements (returns int)

Object

Function Args Description
jv_object_get object, key Get value for key
jv_object_set object, key, value Set key to value
jv_object_delete object, key Remove key
jv_object_has object, key 1 if key exists, 0 otherwise
jv_object_merge object, object Shallow merge (second wins)
jv_object_merge_recursive object, object Deep recursive merge
jv_object_length object Number of keys (returns int)

Equality

Function Args Description
jv_equal a, b 1 if values are equal, 0 otherwise
jv_identical a, b 1 if structurally identical, 0 otherwise
jv_contains a, b 1 if a contains b, 0 otherwise

Path operations

Function Args Description
jv_get value, key Index into array or object
jv_set value, key, new Set value at key
jv_has value, key true/false if key or index exists
jv_getpath value, path Get value at JSON path (array of keys)
jv_setpath value, path, new Set value at JSON path
jv_delpaths value, paths Delete multiple paths

Sorting and grouping

Function Args Description
jv_keys value Sorted keys of object (or indices of array)
jv_keys_unsorted value Keys in insertion order
jv_sort array, keys Sort array by keys
jv_group array, keys Group array elements by keys
jv_unique array, keys Remove duplicates by keys

For jv_sort, jv_group, and jv_unique, pass the array itself as keys to sort/group by value.

String

Function Args Description
jv_string_slice string, start, end Slice string by character index

Testing

make test

Runs the test suite in test.mk (62 assertions).

License

MIT

About

A GNU Make plugin for manipulating JSON data inside Makefiles

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Contributors