0

I am using the json.simple library. And I want to stick to that, if possible. When i retrieve an Object

String blockString = message.get("block").toString();

and then

 System.out.println(blockString)

gives me this

{"type": "block", "transactions": [], "timestamp": "2017-11-02T06:17:05.079481", "reward": "b76364406658d679667e5e4be043f2da9d1f0e4ead17d78fcf152a5d718411c06d5dd2922cdb86a25d09c76dde7d26e8e26a55a90211bc6031f5d3b1cedc5b9c0577c01a1af3b8099261aa83fe6751c317662df130c10df56d4891fd8c1281a5d1bcb45d23b3096384b44a4a7af0e5217b7543eb68660914f936cdcdbe7b4c06851cb0efdef4c3859e7d38ac1da965430d498bcf4854b5d0470a421b2ddd50c61eac3f0e1f80f652daa90b5e1314a84e8fbadc7ad9dd1cdea613d7c292efe64f60d6ea2cbea0153701930a1fbf32d5d28a3d10e85adf8d2390dd7d551204f8fb430d8e0816f347269850751f6b00e23c1f125ff4cda7fd3de723e34aaaaa6599", "difficulty": "0", "nonce": "4c715812ebb33b38ec7546463112a3f9601dd05df13e4124f6ca5c491fb5d5883c941810da5dfa1a9902201af1c1d122476e1544a0ade75f7c05644ff25d8f2c", "parent": "00000f6c2e83499bd66b82128d523b4e6e17f289c938447c26f2edb20d382de818e332bf35349f92a5316657b9fd9a1e7b92b7c2aa709d7f487ea696582eec03"}

which is exactly what I want to have. Now, when I do the following:

JSONObject blockPayLoad = (JSONObject) JSONValue.parse(blockString);

and then

System.out.println(blockPayLoad.toString());

I get this

{"reward":"b76364406658d679667e5e4be043f2da9d1f0e4ead17d78fcf152a5d718411c06d5dd2922cdb86a25d09c76dde7d26e8e26a55a90211bc6031f5d3b1cedc5b9c0577c01a1af3b8099261aa83fe6751c317662df130c10df56d4891fd8c1281a5d1bcb45d23b3096384b44a4a7af0e5217b7543eb68660914f936cdcdbe7b4c06851cb0efdef4c3859e7d38ac1da965430d498bcf4854b5d0470a421b2ddd50c61eac3f0e1f80f652daa90b5e1314a84e8fbadc7ad9dd1cdea613d7c292efe64f60d6ea2cbea0153701930a1fbf32d5d28a3d10e85adf8d2390dd7d551204f8fb430d8e0816f347269850751f6b00e23c1f125ff4cda7fd3de723e34aaaaa6599","difficulty":"0","parent":"00000f6c2e83499bd66b82128d523b4e6e17f289c938447c26f2edb20d382de818e332bf35349f92a5316657b9fd9a1e7b92b7c2aa709d7f487ea696582eec03","type":"block","transactions":[],"nonce":"4c715812ebb33b38ec7546463112a3f9601dd05df13e4124f6ca5c491fb5d5883c941810da5dfa1a9902201af1c1d122476e1544a0ade75f7c05644ff25d8f2c","timestamp":"2017-11-02T06:17:05.079481"}

So two concerns/questions

  1. How can I keep the spaces?
  2. How can I keep the previous order (starting with "type"..etc)?

Reason is, I am hashing the entire thing, and therefore I cannot tamper with whitespaces/order of the objects.

InDaPond
  • 574
  • 3
  • 6
  • 23
  • I don't think the `JSONObject.toString` method makes any guarantees as to the order or whitespace. Why not hash the string after parsing as `JSONObject`? – D.B. Nov 21 '17 at 04:08
  • Reason is I have a class BlockPayLoad which represents the block by having the different Json Keys as fields. So I can access them, compare them, check for validity etc. If I always have to retrieve the fields from the Json Object and then parse them into the right format, it is very hard for others to understand what is happening / not really modular anymore – InDaPond Nov 21 '17 at 06:16
  • What I mean is you are converting the block to a string then parsing that into a `JSONObject`. Why not create your hash *after* you do this by calling `toString` on the `JSONObject` and hashing the result of this call? – D.B. Nov 21 '17 at 17:43
  • Because I get the JSON object from an API-call, therefore it is already created and its order/format has to be kept, since if I want to reconstruct a blockchain I need to verify its hash-value, therefore nothing can be tampered with. – InDaPond Nov 23 '17 at 10:46
  • How about holding onto the original string rather than converting the object back to a string? – D.B. Nov 23 '17 at 19:52
  • Yeah I am currently doing that, it is just annoying, because inside the original string there are other json objects included. So when I want to access their values, it is a messy access and that's why I thought having an own class with easy accessors would increase readibility – InDaPond Nov 24 '17 at 07:52
  • You can have both a json string and an object representation. It would use more memory but if you're only accessing data not changing the data that's probably the simplest solution. – D.B. Nov 24 '17 at 23:13

0 Answers0