This question is answered. Helpful answers available: 1. Correct answers available: 1.


Permlink Replies: 14 - Pages: 1 - Last Post: 08-May-2018 02:03 Last Post By: drmikey
drmikey

Posts: 254
Registered: 22-Dec-2006
Error when adding additional info to json output -duplication of values
Posted: 06-May-2018 21:20
 
  Click to reply to this thread Reply
I have a bit of code in init.bsh to add a unique "ID" to every image in the json output.
Here is the important part:
void addIndexes(JSONObject tree) {
  List children = tree.get("objects");
  int i=1;
  for (JSONObject o : tree.get("objects")) {
    o.put("ID", i++ + "ngid");
    if (o.containsKey("objects")) {
      addIndexes(o);
    }
  }
}
 jsonmaker = new JSONMaker(engine);
 tree = jsonmaker.getTree(rootFolder);
   addIndexes(tree);
   rename(tree);
dataT = tree.toJSONString();


It seemed to be working until I discovered that there are duplicate ID's produced in the json (i.e. 0ngid, 1ngid, etc. show up multiples times) file rather than each being a unique ID.
I'm not sure why I would get duplicates but hopefully someone sees the error of my ways...

Mike
RobM

Posts: 3,065
Registered: 4-Aug-2006
Re: Error when adding additional info to json output -duplication of values
Posted: 06-May-2018 23:53   in response to: drmikey in response to: drmikey
 
  Click to reply to this thread Reply
drmikey wrote:
I have a bit of code in init.bsh to add a unique "ID" to every image in the json output.
Here is the important part:
void addIndexes(JSONObject tree) {
  List children = tree.get("objects");
  int i=1;
  for (JSONObject o : tree.get("objects")) {
    o.put("ID", i++ + "ngid");
    if (o.containsKey("objects")) {
      addIndexes(o);
    }
  }
}
 jsonmaker = new JSONMaker(engine);
 tree = jsonmaker.getTree(rootFolder);
   addIndexes(tree);
   rename(tree);
dataT = tree.toJSONString();

It seemed to be working until I discovered that there are duplicate ID's produced in the json (i.e. 0ngid, 1ngid, etc. show up multiples times) file rather than each being a unique ID.
I'm not sure why I would get duplicates but hopefully someone sees the error of my ways...

Mike

Don’t you need to pass the value of i, starting with i=1 on the first call, each time addIndexes is called? Something like
void addIndexes(JSONObject tree, Integer i) {
 List children = tree.get("objects");
 for (JSONObject o : tree.get("objects")) {
  o.put("ID", i++ + "ngid");
  if (o.containsKey("objects")) {
  addIndexes(o, i);
  }
 }
}
jsonmaker = new JSONMaker(engine);
tree = jsonmaker.getTree(rootFolder);
addIndexes(tree, 1);
rename(tree);
dataT = tree.toJSONString();


Edited by: RobM on 07-May-2018 09:44
drmikey

Posts: 254
Registered: 22-Dec-2006
Re: Error when adding additional info to json output -duplication of values
Posted: 07-May-2018 02:28   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Thanks, but it gives an error. The original bit of code was from David Ekholm. so I guess I should see what he thinks.
RobM

Posts: 3,065
Registered: 4-Aug-2006
Re: Error when adding additional info to json output -duplication of values
Posted: 07-May-2018 12:02   in response to: drmikey in response to: drmikey
 
  Click to reply to this thread Reply
There was a forum quote that got into the earlier code because I copied and pasted your code without removing the '>' characters. The, slightly modified, code below I tried in my Sprint skin's init.bsh and it works. Note I remmed out your 'rename(tree);' line as that did cause an error, I assume it is your own routine.
import org.json.simple.*;
void addIndexes(JSONObject tree, Integer i) {
  List children = tree.get("objects");
  for (JSONObject o : tree.get("objects")) {
    o.put("ID", i + "ngid");
    System.out.println("object " + o);
    if (o.containsKey("objects")) {
    i++;
      addIndexes(o, i);
    }
    i++;
  }
}
 jsonmaker = new JSONMaker(engine);
 tree = jsonmaker.getTree(rootFolder);
   addIndexes(tree, 1);
   //rename(tree);
dataT = tree.toJSONString();
 
System.out.print(dataT);
 
/** -------------------- Json file ----------------- **/
jsonmaker = new JSONMaker(engine);
dataTree = jsonmaker.getTreeAsString(rootFolder);

With a project of one image, then a folder with one image and then another image at the root level i.e. four objects I get the following (truncated to make it easier to see) system output
object {"path":"Bridge.jpg","image","ID":"1ngid","category":"image"}
object {"path":"A%20Folder","ID":"2ngid","category":"folder"}
object {"path":"Mountain.jpg","image","ID":"3ngid","category":"image"}
object {"path":"Leaves.jpg","ID":"4ngid","category":"image"}
drmikey

Posts: 254
Registered: 22-Dec-2006
Re: Error when adding additional info to json output -duplication of values
Posted: 07-May-2018 18:38   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Thanks. No errors and seems to work except for one unique situation. If one has 2 folders and puts an image with the same name in each folder, those 2 separate images have the same ID.

The following is the json output. One image (DSC_0569 (1).jpg) is in the root folder and the duplicate is in the subfolder2 folder and both have the same ID, 4ngid . It's likely most users would not routinely have duplicate image names, but certainly a possibility.

{
   "path":"Gallery1",
   "counters":{
      "total":3,
      "images":2,
      "folders":1,
      "files":2
   },
   "thumb":{
      "path":"thumbs\/DSC_0176.jpg",
      "width":400,
      "height":267
   },
   "objects":[
      {
         "path":"DSC_0176.jpg",
         "image":{
            "path":"slides\/DSC_0176.jpg",
            "width":1200,
            "height":800
         },
         "thumb":{
            "path":"thumbs\/DSC_0176.jpg",
            "width":400,
            "height":267
         },
         "fileSize":8624387,
         "kind":"image",
         "name":"DSC_0176.jpg",
         "fileDate":"2016-05-08T21:10:25.0Z",
         "label":"DSC_0176",
         "ID":"1ngid",
         "category":"image",
         "title":"DSC_0176",
         "camera":{
            "aperture":5.6,
            "exposureTime":"1\/320s",
            "originalDate":"2016-04-17T16:12:09.0Z",
            "cameraModel":"NIKON D5500",
            "focalLength35mm":"450mm",
            "resolution":"3638 x 2425",
            "cameraMake":"NIKON CORPORATION",
            "isoEquivalent":400,
            "flash":"Flash did not fire, auto",
            "focalLength":"300mm"
         }
      },
      {
         "path":"subfolder2",
         "deepCounters":{
            "total":2,
            "images":2,
            "files":2
         },
         "counters":{
            "total":2,
            "images":2,
            "files":2
         },
         "thumb":{
            "path":"subfolder2\/thumbs\/P9062481.jpg",
            "width":400,
            "height":163
         },
         "kind":"album",
         "objects":[
            {
               "path":"P9062481.jpg",
               "image":{
                  "path":"slides\/P9062481.jpg",
                  "width":1200,
                  "height":488
               },
               "thumb":{
                  "path":"thumbs\/P9062481.jpg",
                  "width":400,
                  "height":163
               },
               "fileSize":2208045,
               "kind":"image",
               "name":"P9062481.jpg",
               "fileDate":"2016-10-20T21:56:19.0Z",
               "label":"P9062481",
               "ID":"3ngid",
               "category":"image",
               "title":"Version 2",
               "camera":{
                  "aperture":6.3,
                  "exposureTime":"1\/250s",
                  "originalDate":"2010-09-06T06:49:04.0Z",
                  "cameraModel":"E-420           ",
                  "resolution":"3648 x 1484",
                  "cameraMake":"OLYMPUS IMAGING CORP.  ",
                  "isoEquivalent":100,
                  "flash":"Flash did not fire, auto",
                  "focalLength":"150mm"
               }
            },
            {
               "path":"DSC_0569%20%281%29.jpg",
               "image":{
                  "path":"slides\/DSC_0569%20%281%29.jpg",
                  "width":1200,
                  "height":816
               },
               "thumb":{
                  "path":"thumbs\/DSC_0569%20%281%29.jpg",
                  "width":400,
                  "height":272
               },
               "fileSize":994577,
               "kind":"image",
               "name":"DSC_0569 (1).jpg",
               "fileDate":"2018-05-06T07:58:25.0Z",
               "label":"DSC_0569 (1)",
               "ID":"4ngid",
               "category":"image",
               "title":"Version 2",
               "camera":{
                  "aperture":7.1,
                  "exposureTime":"1\/60s",
                  "originalDate":"2016-04-17T18:38:28.0Z",
                  "cameraModel":"NIKON D5500",
                  "focalLength35mm":"117mm",
                  "resolution":"4577 x 3112",
                  "cameraMake":"NIKON CORPORATION",
                  "isoEquivalent":2000,
                  "flash":"Flash did not fire, auto",
                  "focalLength":"78mm"
               }
            }
         ],
         "name":"subfolder2",
         "fileDate":"2018-05-07T08:26:33.0Z",
         "label":"subfolder2",
         "ID":"2ngid",
         "category":"folder",
         "title":"subfolder2"
      },
      {
         "path":"DSC_0569%20%281%29.jpg",
         "image":{
            "path":"slides\/DSC_0569%20%281%29.jpg",
            "width":1200,
            "height":816
         },
         "thumb":{
            "path":"thumbs\/DSC_0569%20%281%29.jpg",
            "width":400,
            "height":272
         },
         "fileSize":995304,
         "kind":"image",
         "name":"DSC_0569 (1).jpg",
         "fileDate":"2018-05-07T08:27:12.0Z",
         "label":"DSC_0569 (1)",
         "ID":"4ngid",
         "category":"image",
         "title":"Version 2",
         "camera":{
            "aperture":7.1,
            "exposureTime":"1\/60s",
            "originalDate":"2016-04-17T18:38:28.0Z",
            "cameraModel":"NIKON D5500",
            "focalLength35mm":"117mm",
            "resolution":"4577 x 3112",
            "cameraMake":"NIKON CORPORATION",
            "isoEquivalent":2000,
            "flash":"Flash did not fire, auto",
            "focalLength":"78mm"
         }
      }
   ],
   "name":"Gallery1",
   "comment":"This is a test",
   "fileDate":"2018-05-07T08:27:11.0Z",
   "label":"Gallery1",
   "title":"Gallery1"
}
RobM

Posts: 3,065
Registered: 4-Aug-2006
Re: Error when adding additional info to json output -duplication of values
Posted: 07-May-2018 20:53   in response to: drmikey in response to: drmikey
 
  Click to reply to this thread Reply
I'm struggling to reproduce that, could you zip up the project folder so I can try exactly what you are doing?
drmikey

Posts: 254
Registered: 22-Dec-2006
Re: Error when adding additional info to json output -duplication of values
Posted: 07-May-2018 21:26   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Sure. Thanks for taking a look.
I partially fixed my issue with duplicate images by adding fileSize along with the value of ID, to try an make it unique for each image. I will send a version of the skin with just the ID value. Also, it is a very complicated update to my Nano skin which uses a handlebars template and the json data and still has some bugs. (i.e. a work in progress).
The most difficult part is getting the containing folder's json info, so there are some handlebars partial templates that get that. (It would be so much easier if the json data included each images containing folder info!)

Mike
Also, lots of extra code in there that I am hacking around with, so beware.
RobM

Posts: 3,065
Registered: 4-Aug-2006
Re: Error when adding additional info to json output -duplication of values
Posted: 07-May-2018 21:35   in response to: drmikey in response to: drmikey
 
  Click to reply to this thread Reply
drmikey wrote:
Sure. Thanks for taking a look.
I partially fixed my issue with duplicate images by adding fileSize along with the value of ID, to try an make it unique for each image. I will send a version of the skin with just the ID value. Also, it is a very complicated update to my Nano skin which uses a handlebars template and the json data and still has some bugs. (i.e. a work in progress).
The most difficult part is getting the containing folder's json info, so there are some handlebars partial templates that get that. (It would be so much easier if the json data included each images containing folder info!)

Mike
Also, lots of extra code in there that I am hacking around with, so beware.

Sorry, I meant the album project not the skin. I tried a project with a root image and 2 subfolders, 1 image in each folder and all images with the same name. That worked.
I then tried moving one of the folders into the other - one root image, one folder with an image and a subfolder with an image. Again it worked with all images having the same name.
drmikey

Posts: 254
Registered: 22-Dec-2006
Re: Error when adding additional info to json output -duplication of values
Posted: 07-May-2018 22:42   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Sorry, misread the request. Very weird that I get the same ID for two images.
Here's the Project

project
RobM

Posts: 3,065
Registered: 4-Aug-2006
Re: Error when adding additional info to json output -duplication of values
Posted: 07-May-2018 22:52   in response to: drmikey in response to: drmikey
 
  Click to reply to this thread Reply
drmikey wrote:
Sorry, misread the request. Very weird that I get the same ID for two images.
Here's the Project

project

Its asking for a username and password :(
drmikey

Posts: 254
Registered: 22-Dec-2006
Re: Error when adding additional info to json output -duplication of values
Posted: 07-May-2018 22:54   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Sorry, try this:

Project
RobM

Posts: 3,065
Registered: 4-Aug-2006
Re: Error when adding additional info to json output -duplication of values
Posted: 07-May-2018 22:56   in response to: drmikey in response to: drmikey
 
  Click to reply to this thread Reply
drmikey wrote:
Sorry, try this:

Project

Better, give me a hour or so, I'm a bit busy on other things just now.
RobM

Posts: 3,065
Registered: 4-Aug-2006
Re: Error when adding additional info to json output -duplication of values
Posted: 07-May-2018 23:08   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
OK, I get the same result now, need a bit of time to look at why.
RobM

Posts: 3,065
Registered: 4-Aug-2006
Re: Error when adding additional info to json output -duplication of values
Posted: 08-May-2018 00:11   in response to: RobM in response to: RobM
Helpful
  Click to reply to this thread Reply
Here you go, what was missing was incrementing the counter on exiting the folder. I changed the declaration of Integer to int as you don't need a null value.
//in init.bsh add this to inject an ID to the JASON data
import org.json.simple.*;
 
void addIndexes(JSONObject tree, int i) {
  List children = tree.get("objects");
  for (JSONObject o : tree.get("objects")) {
    o.put("ID", i + "ngid");
    if (o.containsKey("objects")) {
    	i++;
    	addIndexes(o, i);
    	i++;
    }
    i++;
  }
}
 jsonmaker = new JSONMaker(engine);
 tree = jsonmaker.getTree(rootFolder);
   addIndexes(tree, 1);
   //rename(tree);
dataT = tree.toJSONString();
drmikey

Posts: 254
Registered: 22-Dec-2006
Re: Error when adding additional info to json output -duplication of values
Posted: 08-May-2018 02:03   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Awesome, Thanks for doing this!!! Works as expected.

Mike

Legend
Forum admins
Helpful Answer
Correct Answer

Point your RSS reader here for a feed of the latest messages in all forums