Error parsing empty json array with nashorn - javascript

I'm using Oracle JDK 1.8.0_65 with nashorn to run some test cases and I found a very strange behavior when parsing an empty JSON Array.
Here's the code i'm running in nashorn:
var testCase = {
start:function() {
// Case 1: a initialized from JavaScript Array
var a = [];
this.log.debug("a before:" + JSON.stringify(a) + " (length:" + a.length + ")");
a.push(15);
this.log.debug("a after:" + JSON.stringify(a) + " (length:" + a.length + ")");
// Case 2: b initialized parsing a JSON Array
var b = JSON.parse("[]");
this.log.debug("b before:" + JSON.stringify(b) + " (length:" + b.length + ")");
b.push(15);
this.log.debug("b after:" + JSON.stringify(b) + " (length:" + b.length + ")");
}
};
and the output is:
a before:[] (length:0)
a after:[15] (length:1)
b before:[] (length:0)
b after:[0,15] (length:2)
I'ts look like a bug in nashorn JSON parser. Returned Array is not really an empty Array, but it's look like that before pushing the first element. There's a hidden "0" that appears after the first push.
Can't find any bug report about this behavior.
Am I using JSON.parse in a wrong way?
Thanks.
J

Your usage is correct. It seems to be a bug and it appears to have been fixed. I just tried 1.8.0_112. It works as expected.

Related

Why is the following way to assign a value to a JSON array not working?

I have this code:
compareList[productName] = productID + ',' + productHref;
console.log(productName + ' ' + productID + ' ' + productHref + ' ' + compareList.length);
Which logs into this (I have removed the link):
Acer Iconia B1-790 [NT.LDFEE.002] 112576 link removed for confidentiality 0
As you can see, all three variables are valid strings, but the json object still fails to assign (compareList.length logs as 0). I've been thinking and thinking but I simply can't figure it out. Any help is appreciated.
Maybe this version of adding and checking array length can be useful to you?
var compareList=[]
var productName = {productID:'saban',productHref:'http://saulic.com'};
compareList.push(productName);
console.log(compareList.length);

QZ Tray raw Printing

This is my very first question.
How can i run an "IF STATEMENT" in side the raw code of QZ tray where Var = print data [];
The below code works wonderful without IF STATEMENT, but the codes cannot parse once i use it.
var printData = [
'<xpml><page quantity="0" pitch="127.0 mm"></xpml>^AD\n',
'^O0\n'
'<xpml></page></xpml><xpml><page quantity="9" pitch="127.0 mm"></xpml>~MDELF,FORMAT_0\n',
'^E10.0\n',
'^L\n',
'C0,0000000000000000,+1,prompt_C0\n',
'C1,0000000000000000,+1,prompt_C1\n',
'C2,000,+1,prompt_C2\n',
'Lo,51,438,761,440\n',
'Lo,51,678,761,680\n',
'Lo,51,558,761,560\n',
'Lo,51,158,761,160\n',
'AH,320,31,1,1,0,0,'+ acs +'\n',
'BQ2,160,742,4,8,156,0,0,C^C0\n',
'AD,254,900,1,1,0,0,^C1\n',
'AA,439,440,1,1,0,0,Service\n',
'Lo,425,440,427,678\n',
'AA,442,560,1,1,0,0,Total No of Pieces\n',
'AA,439,684,1,1,0,0,Origin\n',
'AB,511,684,1,1,0,0,' + origin +'\n',
'AF,182,590,1,1,0,0,'+ destination+'\n',
'R49,13,762,999,3,3\n',
'E\n',
'^KFORMAT_0\n',
if (pcstart.length ==1)
{
premawb + postmawb +'0000'+ pcstart +'\n',
}
else {
premawb + postmawb +'000'+ pcstart +'\n',
}
pcstart + '\n',
'E\n',
'~P'+ copyPrint+'\n',
qz.print(config, printData).catch(displayError);
}
How can i run an "IF STATEMENT" in side the raw code of QZ tray
You can't mid-array, but you can add a ternary operator which does the same thing for a simple if/else statement:
pcstart.length == 1 ? '0000' : '000'
... and in context...
var printData = [
'<xpml><page quantity="0" pitch="127.0 mm"></xpml>^AD\n',
'...',
'^KFORMAT_0\n',
premawb + postmawb + (pcstart.length == 1 ? '0000' : '000') + pcstart + '\n',
pcstart + '\n',
'E\n',
'~P'+ copyPrint + '\n'
];
qz.print(config, printData).catch(displayError);
You can also call a function on the array element, so you may find it more desirable to roll your own pad(...) function and then call pad on the entire number or concatenated string... e.g:
premawb + postmawb + pad(pcstart, 4) +' \n',
I the above example, pad(...) is a function you make that can contain all the if/else statements you need and returns the formatted value.

Javascript syntax error? "Unexpected Indentifier"

sorry I just started with javascrippt and html and I am having trouble finding out what the unexpected identifier is?
here is my function
function fillSearchPlayer(data){
$('#searchPlayers tbody').html('');
for (var i = data.length - 1; i >= 0; i--) {
var p = data[i]
var item = '<tr><td>'p.firstname + ' ' + p.lastname'</td><td>'p.job'</td><td><button class="cbtn" onclick="requestPlayer("'+ p.identifier + '")">More</button></td></tr>'
$('#searchPlayers tbody').append('item');
}
}
maybe you guys could help me, it's saying its coming from the line that starts with "var item"
'<tr><td>'p.firstname look like you missed a plus sign over there. The other thing is .append('item'); - you probably intented to do .append(item);.
As the guys mentioned in the direct comments above you should try to use some templating engine instead of constructing the strings the way you did it.
I would recommend you to read these pieces:
Handlebars - simple and convenient template engine in JavaScript - give it a try!
Template strings in ES2016 - a cleaner way to do what you did with manual string concatenation
There are several errors. String concatenation works with +, and you're missing a semicolon.
Try:
function fillSearchPlayer(data) {
$('#searchPlayers tbody').html('');
for (var i = data.length - 1; i >= 0; i--) {
var p = data[i];
var item = '<tr><td>' + p.firstname + ' ' + p.lastname + '</td><td>' + p.job + '</td><td><button class="cbtn" onclick="requestPlayer("'+ p.identifier + '")">More</button></td></tr>';
$('#searchPlayers tbody').append('item');
}
}
The var item = ... item mixes variable and literal definitions without the real concatenation going on. Just put a + between each varying element to achieve your goal.
You need to enclose the concatenation operator around the p.job variable.
Your assignment should look like this...
var item = '<tr><td>'+p.firstname + ' ' + p.lastname+'</td><td>'+p.job+'</td><td><button class="cbtn" onclick=requestPlayer('+ p.identifier + ')>More</button></td></tr>'

Count that object have another object in javascript?

My object data is as follows:
var object = '({10:{id:"10", v_title:"1", ' +
'13:{id:"13", v_title:"1.1", ' +
'15:{id:"15", v_title:"1.1.1 ", v_noofpara:"g2 ", v_description:"d1.1.1"}' +
'}'+
'},'+
'11:{id:"11", v_title:"2", ' +
'14:{id:"14", v_title:"2.1", ' +
'16:{id:"16", v_title:"2.1.1 ", v_noofpara:"g1 ", v_description:"des2.1.1 "}' +
'}' +
'}' +
'})'
Note that the key value 10(Grand Parent) has 13(parent) and 13 has 15(child). Similarly 11(parent) has 14 and 14 has 16.
Using hasOwnProperty or any other methods can any one give me the count with relationship so that use it further.
Since you are not dealing with a valid JSON structure, you have to run it through eval:
var myObject = eval(object);
Now you have a valid Javascript Object that you can loop through to get the count of each child or the sum of each first child etc. as you please.
Refer to this StackOverflow question about how to get the length of an object. Answers with and without jQuery are provided.
You can use eval to convert it to an object:
var o = eval(object);
Now you can use for..in to iterate over the properties. You can test the value of each property and if it's an object, do for..in on it too, ad infinitum.

Javascript - concat string does not work as expected

What's the wrong with this Javascript line?
user: h.reem
domain: somedomain
var target = "//account/win/winlogin.aspx" +
"?username=" +
user.toString() +
"&domain=" +
domain.toString();
the resutl is always:
//account/win/winlogin.aspx?username=h.reem
Any idea!!
alert(user + "X") shows only h.reem
The ActiveX component is probably returning a null terminated string (I've seen this with Scripting.TypeLib & a couple of the AD objects for example) so concatenating it with another string fails. (You can verify this if 0 === user.charCodeAt(user.length - 1)).
You will need remove the last character before using the string;
user = user.substr(0, user.length - 1);
try:
var sUser = user.toString();
var sDomain = domain.toString();
var target = "//account/win/winlogin.aspx" + "?username=" + sUser + "&domain=" + sDomain;
The above might not fix your problem but it should expose it - Could be that your user.toString() method isn't returning a string and is short-circuiting things... If this doesn't answer your question I'd be glad to assist further, but it would be helpful if you posted the implementation or source of "user" somewhere ...

Categories