Skip to content

Instantly share code, notes, and snippets.

@alexmherrmann
Last active January 31, 2016 23:21
Show Gist options
  • Save alexmherrmann/f0441404497d5be4ba58 to your computer and use it in GitHub Desktop.
Save alexmherrmann/f0441404497d5be4ba58 to your computer and use it in GitHub Desktop.
JSON/MD5 problems
{
"name": "dansdedupe",
"description": "one off script to rename and dedupe dans files",
"authors": ["Alex Herrmann"],
"targetType": "executable",
"dependencies": {
"std_data_json": "0.17.0"
}
}
import std.stdio;
import std.container.rbtree;
import std.digest.md : md5Of, toHexString;
import std.datetime : DateTime, SysTime;
import std.parallelism;
import std.file;
// import std.json;
import stdx.data.json;
import std.range : chunks;
import std.array : array;
import std.algorithm : map;
import std.conv : parse;
import std.format : format;
public string str(in JSONValue injv) {
return injv.toJSON();
}
public string toString(in JSONValue injv) {
return injv.toJSON();
}
public string toPrettyString(in JSONValue injv) {
return injv.toJSON();
}
void err(in string inst) {
stderr.writeln(inst);
}
void log(in string inst) {
stdout.writeln(inst);
}
struct filedata {
DateTime modtime;
string filename;
ubyte[16] md5_hash;
JSONValue fdToJson() {
JSONValue[string] obj;
auto outtime = JSONValue(modtime.date.toISOExtString());
/*err("mtm: %s".format(outtime.toString()));*/
obj["modtime"] = outtime;
auto fname = JSONValue(filename);
/*err("fnm: %s".format(fname.toString()));*/
obj["filename"] = fname;
auto outmd5 = JSONValue(cast(string) toHexString(md5_hash));
/*err("md5: %s".format(outmd5.toString()));*/
obj["md5"] = outmd5;
auto jav = JSONValue(obj);
err("tojson created:\n%s".format(jav.toString())); // works just fine here
return jav;
}
unittest {
filedata a;
a.modtime = DateTime(1999,10,10);
a.filename = "ayy";
a.md5_hash = md5Of("lmao");
auto json = a.fdToJson();
log("Json: %s".format(json.toString())); // Not here though
log("ran filedata tojson test");
}
}
void main() {}
@alexmherrmann
Copy link
Author

after replacing std.json with stdx.data.json;

Performing "unittest" build using dmd for x86_64.
taggedalgebraic 0.9.4: building configuration "library"...
std_data_json 0.17.0: building configuration "library"...
dansdedupe ~master: building configuration "application"...
Linking...
Running ./dansdedupe 
tojson created:
{
    "md5": "0F18FD4CF40BFB1DEC646807C7FA5522",
    "filename": "ayy",
    "modtime": "1999-10-10"
}
Json: {
    "md5": "�U���\u0000\u0000�U���\u0000\u0000�U���\u0000\u0000\u0017�N\u0000\u0000\u0000\u0000\u0000",
    "filename": "ayy",
    "modtime": "1999-10-10"
}
ran filedata tojson test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment