json_sorter 0.1.0 copy "json_sorter: ^0.1.0" to clipboard
json_sorter: ^0.1.0 copied to clipboard

outdated

Command-line tool to sort JSON files by key and reusable sorted JSON encoder.

json_sorter #

Command-line tool to sort JSON files by key and reusable sorted JSON encoder.

With this package you can sort JSON files by key in-place using the command-line tool provided by the package.

The package also exposes JsonSortedEncoder which very similar to the JsonEncoder, except it first creates a copy of the passed in object and then recursively sorts every map by key.

Ideal if your team is working with JSON files and you want to keep the JSON files organized and you want to do that automatically.

Just imagine a team where every second pull request comment is about someone failing to sort the JSON file correctly... ...and just imagine this team not automating this task in five minutes rather they spend the same amount of time in every pull request nagging each other to sort the damn JSON file.

I hope this does not sound familiar to you, but in case it does, now you can pull in this package to do everything for you automatically.

You can use the JsonSortedEncoder if you would like to make sure that in your app, all JSON output (be it files or HTTP responses) is sorted.

Continuous Integration codecov json_sorter Published by dartside.dev GitHub Stars Count

Usage #

json_sorter command-line tool #

You can run Dart scripts from your dependencies using the pub run command.

  1. Add json_sorter to your dev_dependencies: pub run add -d json_sorter.
  2. Run the script pub run json_sorter --space-indent 2 filename.json

Keep in mind that how you invoke the json_sorter, depends on how you install and whether you are using it from a Flutter or Dart project.

Install globally pub global activate json_sorter, then you can execute it simply by typing json_sorter example.json.

For more info, execute json_sorter -h.

JsonSortedEncoder #

If you want to create sorted JSONs in your file, for example in your server's responses, when you print a JSON response in your logs, or when you work with Dart scripts that manipulate JSON files, you can use the JsonSortedEncoder.

It works similarly to the JsonEncoder from dart:convert.

If you don't need indentation, use simply the JsonSortedEncoder constructor. If you need indentation, you can use the JsonSortedEncoder.withIndent(indent) constructor.

// After you added json_sorter to your dependencies
// $ dart pub add json_sorter
import 'package:json_sorter/json_sorter';

// Create encoder
const encoder = JsonSortedEncoder.withIndent('  ');

// Use it on any JSON serializable object, for example lists and maps
const example = {'b': true, 'a': false};

// Use the convert method
const asJson = encoder.convert(example);

// Enjoy
print(asJson);

jsonSortedEncode #

This handy function works exactly like the jsonEncode from dart:convert, the only difference is that it sorts the map keys recursively in the object before encoding.

import 'package:json_sorter/json_sorter.dart';

const inputMap = {
  'xxx': {
    'r': 'r',
    'rr': 'rr',
    'q': 'q',
  },
  'aaa': [1, 1, 1],
};

void main() {
    print(jsonSortedEncode(inputMap));
}

example #

Don't forget the project's example folder for more, executable examples.

8
likes
0
pub points
70%
popularity

Publisher

verified publisherdartside.dev

Command-line tool to sort JSON files by key and reusable sorted JSON encoder.

Homepage
View/report issues

License

unknown (LICENSE)

Dependencies

args

More

Packages that depend on json_sorter