Skip to content

Instantly share code, notes, and snippets.

@alexmherrmann
Last active January 31, 2016 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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

line 34 is where I get a problem full output of command is this:

$ rdmd -unittest test.d 
tojson created:
{
    "filename": "ayy",
    "md5": "0F18FD4CF40BFB1DEC646807C7FA5522",
    "modtime": "1999-10-10"
}
core.exception.UnicodeException@src/rt/util/utf.d(290): invalid UTF-8 sequence
----------------
??:? onUnicodeError [0x4c2c91]
??:? dchar rt.util.utf.decode(const(char[]), ref ulong) [0x4a8779]
??:? _aApplycd1 [0x4a3960]
??:? void std.json.toJSON(const(std.json.JSONValue*), const(bool), const(std.json.JSONOptions)).toString(immutable(char)[]) [0x4b3838]
??:? void std.json.toJSON(const(std.json.JSONValue*), const(bool), const(std.json.JSONOptions)).toValue(const(std.json.JSONValue*), ulong) [0x4b3baa]
??:? _D3std4json6toJSONFxPS3std4json9JSONValuexbxE3std4json11JSONOptionsZ7toValueMFxPS3std4json9JSONValuemZ14__T4emitTAAyaZ4emitMFAAyaZv [0x4b416c]
??:? void std.json.toJSON(const(std.json.JSONValue*), const(bool), const(std.json.JSONOptions)).toValue(const(std.json.JSONValue*), ulong) [0x4b3a9f]
??:? immutable(char)[] std.json.toJSON(const(std.json.JSONValue*), const(bool), const(std.json.JSONOptions)) [0x4b37dc]
??:? const(immutable(char)[] function(const(std.json.JSONOptions))) std.json.JSONValue.toString [0x4b378b]
??:? void test.filedata.__unittestL47_1() [0x4928a5]
??:? void test.__modtest() [0x49ff58]
??:? int core.runtime.runModuleUnitTests().__foreachbody2(object.ModuleInfo*) [0x4c2fce]
??:? int object.ModuleInfo.opApply(scope int delegate(object.ModuleInfo*)).__lambda2(immutable(object.ModuleInfo*)) [0x4a2387]
??:? int rt.minfo.moduleinfos_apply(scope int delegate(immutable(object.ModuleInfo*))).__foreachbody2(ref rt.sections_elf_shared.DSO) [0x4a78f6]
??:? int rt.sections_elf_shared.DSO.opApply(scope int delegate(ref rt.sections_elf_shared.DSO)) [0x4a7985]
??:? int rt.minfo.moduleinfos_apply(scope int delegate(immutable(object.ModuleInfo*))) [0x4a7887]
??:? int object.ModuleInfo.opApply(scope int delegate(object.ModuleInfo*)) [0x4a2363]
??:? runModuleUnitTests [0x4c2e69]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll() [0x4a43fe]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x4a4394]
??:? _d_run_main [0x4a42f1]
??:? main [0x49ffed]
??:? __libc_start_main [0x19d78a3f]

@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