News for hackers. Stuff that matters.
Re:JavaScript is Good, Actually by worldsayshi on Wednesday May 16, 2018 @02:28AM (#17080392)
Switching back to Java after having spent a long time in JavaScript land with modern standards made me realize how great JavaScript had become.

The biggest reason for JavaScripts greatness to me is JSON. I couldn't understand how Java developers put up with such bulky ways to deal with data. After some time in Java land I've come back to appreciating its strengths again though. Code completion is nice. I'm kind of hoping for more typescript in the future.

Re:JavaScript is Good, Actually by realharo on Wednesday May 16, 2018 @04:13AM (#17080769)
You should try Kotlin. It solves exactly this pain point - creating classes for data structures is very terse and easy.

You can have a one-liner class like

    class Person(val name: String, val email: String, val yearOfBirth: Int? = null)
Or you can make it a `data class` and get automatic `equals` and other things.
Re:JavaScript is Good, Actually by worldsayshi on Wednesday May 16, 2018 @09:39AM (#17082258)
Thank you for telling me. I've been hovering around this possibility since I'm using Intellij and am the primary owner of the code. Although I'm also keen on this code being as accessible as possible for the rest of the organisation and few has even heard of Kotlin.

When I suggest switching to Kotlin everyone's always smiling dismissively like "ah, you with your crazy ideas again".

Might do it though. Maybe.

Re:JavaScript is Good, Actually by ben509 on Wednesday May 16, 2018 @10:05AM (#17082444)
Don't ask, do it. Asking is a great way to get shot down. Write something smaller in Kotlin, then show people what it looks like. It is heavily interoperable with Java, so you could move some of the boilerplate heavy classes over.
Re:JavaScript is Good, Actually by sbalea on Wednesday May 16, 2018 @07:40AM (#17081590)
Concepts borrowed (in a simplified form) from Scala.
Re:JavaScript is Good, Actually by noicebrewery on Wednesday May 16, 2018 @02:58AM (#17080498)
JSON has its strengths and weaknesses, and while Java doesn't have first class support for it the Jackson libraries are excellent!
Re:JavaScript is Good, Actually by rjbwork on Wednesday May 16, 2018 @04:14AM (#17080773)
Unfortunately given that java does type erasure, you don't get the kind of full featured JSON lib that Json.Net is in the .NET world. Presumably you'd have to somehow annotate to the deserializer what each individual list/array's generic type is for it to properly deserialize, separate from the type definition itself.
Re:JavaScript is Good, Actually by BlackFly on Wednesday May 16, 2018 @04:41AM (#17080886)
I'm not sure what scenario you are envisioning. Type erasure does not completely eradicate generic type information, reflection can still be used to infer generic usage for deserialization and dependency injection (and is done in Jackson and Spring). To make your life easier in doing so, there is:

https://github.com/jhalterman/typetools

Re:JavaScript is Good, Actually by tannhaeuser on Wednesday May 16, 2018 @04:42AM (#17080890)
I'm using JSON all the time, but it's hardly the be-all/end-all solution for even the basic use case of service payload serialization, let alone config file syntax or document syntax. To begin with, JSON lacks types for URLs, dates/times, and spatial data; it also lacks conventions for error messages and data graphs other than trees, etc. So I think Java not integrating JSON literals at the language level or some such actually is a plus.
Re:JavaScript is Good, Actually by hajile on Wednesday May 16, 2018 @05:04PM (#17086474)
I think that lack is a strength. Lists, Maps, Booleans, Numbers, Strings. With these powers combined, you can create basically anything you can think of. Dates can be unix timestamps or ISO strings which are universal. Spatial data is a compound datatype anyway (plus, there are quite a few data representations depending on number of dimensions, cartesian vs polar, etc). The one datatype that JS could potentially benefit from is a byte array type, but even that can be accomplished with base64 "strings" or Lists of numbers.

To me, it's much better to let every company send error messages in the format that makes sense to them rather than having the One True Error that doesn't quite work for anyone.

My biggest complaints about JSON are syntactic. Optional comments, multi-line strings, and trailing commas would do wonders for day-to-day readability without significantly bloating the specs.

Re:JavaScript is Good, Actually by tannhaeuser on Thursday May 17, 2018 @04:31AM (#17089724)
Agree with your points regarding comments and multi-line strings; don't like optional commas, though.

With respect to your "lack is strength" argument, I can't help but find this unconvincing. We all know JSON is derived from JavaScript object/array literal syntax; your argument is akin to retro-fitting the requirements for service payload serialization to the suboptimal de-facto situation with JSON.

Re:JavaScript is Good, Actually by sgift on Wednesday May 16, 2018 @03:06AM (#17080541)
> Switching back to Java (...)

I miss the "and a project which doesn't use modern standards"-part in your post. It's not as if JSON or other modern things are not used in many Java projects. Your project seems to be stuck in the past, as are many JS projects. Legacy code is no fun most of the time.

Re:JavaScript is Good, Actually by kybernetikos on Wednesday May 16, 2018 @03:14AM (#17080563)
I didn't read this as a json vs xml comment, although I suppose it could have been. Rather, I think it's a comment about how there is no convenient, lightweight syntax in Java for dealing with records/maps/trees/dictionaries.
Re:JavaScript is Good, Actually by worldsayshi on Wednesday May 16, 2018 @09:46AM (#17082297)
Yes this was closer to my point. Dealing with JSON (like data) is much less convenient in Java than JavaScript. Haven't had to deal with XML configuration in Java for a long time, which I'm thankful for.
Re:JavaScript is Good, Actually by sametmax on Wednesday May 16, 2018 @02:57AM (#17080495)
JSON is seldom JS now, as any language has had encoder/decoder built in for years.
Re:JavaScript is Good, Actually by alephnan on Wednesday May 16, 2018 @03:18AM (#17080576)
Sure, but there is boilerplate and abstractions that are needed in a language like Java or Scala. In JavaScript, it's a first-class citizen of the language.
Re:JavaScript is Good, Actually by Scarblac on Wednesday May 16, 2018 @03:28AM (#17080617)
It seems you're not talking about JSON (the serialization format) but rather about Javascript's object literal syntax that inspired JSON. They're really different things.
Re:JavaScript is Good, Actually by Vinnl on Wednesday May 16, 2018 @04:06AM (#17080749)
An advantage, however, is that you can directly encode Javascript data objects to JSON without having to annotate its fields or anything. I don't recall you being able to do that with Java.
Re:JavaScript is Good, Actually by imtringued on Wednesday May 16, 2018 @10:55AM (#17082882)
You can't do that with javascript either unless your data objects are acyclic and if they are then using a JSON parser for Java is equally trivial.
Re:JavaScript is Good, Actually by Vinnl on Thursday May 17, 2018 @02:15AM (#17089171)
You mean that if they are acyclic, you don't have to annotate the fields in Java either? Because the data objects I want to serialize usually are acyclic.
Re:JavaScript is Good, Actually by wyattpeak on Wednesday May 16, 2018 @04:06AM (#17080748)
Perhaps, but probably not. ES6 recognises the distinction, and includes native functions for serialisation. That's first-class treatment if anything is.
Re:JavaScript is Good, Actually by Scarblac on Wednesday May 16, 2018 @05:10AM (#17080979)
JSON.stringify and JSON.parse? They're standard library functions that many languages have, I don't see what's ES6 about them, they're much older.

First class treatment would be if the language had JSON-typed objects with functionality, e.g. something like

    const myJson = j'{"key":"value"}';
    const value = myJson['key'];
    const newJson = myJson.set("otherkey", "newValue");
(using the j'' notation for a hypothetical json-typed value)

But AFAIK it doesn't, not even in ES6.

Re:JavaScript is Good, Actually by brlewis on Wednesday May 16, 2018 @08:20AM (#17081772)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Implementing j`{"key":"value"}`; as a tagged template literal is left as an exercise for the reader.

Re:JavaScript is Good, Actually by Sean1708 on Wednesday May 16, 2018 @05:07AM (#17080968)
Isn't comparing JS to something like Java or Scala a bit misleading in this regard? They're completely different styles of language with different tradeoffs. I think it would be a lot more fair to compare to something like Python or Ruby, in which case this advantage become a lot less clear IMO.
Re:JavaScript is Good, Actually by sametmax on Wednesday May 16, 2018 @06:38AM (#17081323)
Well you must compare a dynamic language with another dynamic language. It would be more fair to compare JS to Python or Ruby if you talk about ease of use.
Re:JavaScript is Good, Actually by osrec on Wednesday May 16, 2018 @02:40AM (#17080428)
I too like JSON, however it is somewhat inefficient for arrays, where you end up repeating keys a lot. If you're gzipping, it probably doesn't matter as much...
Re:JavaScript is Good, Actually by tim333 on Wednesday May 16, 2018 @03:37AM (#17080647)
I'm slightly puzzled. Array = [[1,2,3],[4,5,6]] seems valid json and I'm not sure what's inefficient or where the keys come in?
Re:JavaScript is Good, Actually by osrec on Wednesday May 16, 2018 @05:04AM (#17080958)
I was thinking of arrays of objects, where the keys are repeated for each object in the array.
Re:JavaScript is Good, Actually by Secretmapper on Wednesday May 16, 2018 @06:09AM (#17081211)
I'm still a bit confused how this is a problem. Can you elaborate?

Is there any serialisation standard that specifically addresses this? I know YAML and TOML doesn't at least (correct me if I"m wrong)

Re:JavaScript is Good, Actually by SigmundA on Wednesday May 16, 2018 @06:30AM (#17081288)
Something like Protobuf where there is a schema is defined means metadata like key name does not get repeated for every object, its only the values.

No more trying to have short names to keep file size down.

Re:JavaScript is Good, Actually by egeozcan on Wednesday May 16, 2018 @08:39AM (#17081878)
IMHO, it sounds like the job of the compression algorithm, not the format.
Re:JavaScript is Good, Actually by imtringued on Wednesday May 16, 2018 @11:09AM (#17083006)
Then use compression. gzipped json is highly efficient over the wire.

https://blog.octo.com/en/protocol-buffers-benchmark-and-mobi...

The primary purpose of binary data formats is that they can be parsed quickly and some even allow direct access without a parsing step.

Re:JavaScript is Good, Actually by osrec on Wednesday May 16, 2018 @11:32AM (#17083206)
That's what I said in my first comment, no?
Re:JavaScript is Good, Actually by zinckiwi on Wednesday May 16, 2018 @03:52AM (#17080694)
s/arrays/"collections of homogenous objects" and it's a fair point.
Re:JavaScript is Good, Actually by dvfjsdhgfv on Wednesday May 16, 2018 @03:44AM (#17080671)
If you don't need to exchange the data with other apps, serialization in Java is actually quite decent.
Re:JavaScript is Good, Actually by rusk on Wednesday May 16, 2018 @04:23AM (#17080801)
You still need to make `ObjectStream os = new ObjectInputStream(new FileInputStream(filename))); MyObject mo = (MyObject) os.readObject();` which is the most basic form I can think of (you've got to put in all them bloody `try/catches` as well don't forget, along with `public class MyClass { public static void main(String[] args) { ...` etc. etc.).

Whereas javascript you can just go `require(myjsonfile)`

I love java, but Javascript is indeed great for some of these kinds of things :-)

Re:JavaScript is Good, Actually by cpburns2009 on Wednesday May 16, 2018 @12:14PM (#17083551)
> Whereas javascript you can just go `require(myjsonfile)`

I'm not current on Node features, but that looks dangerous to do. Is that equivalent to reading the file and `eval()`ing it?

Re:JavaScript is Good, Actually by kbp on Wednesday May 16, 2018 @04:25PM (#17086134)
> I'm not current on Node features, but that looks dangerous to do. Is that equivalent to reading the file and `eval()`ing it?

No:

    > const fs = require('fs')
    undefined
    > fs.writeFileSync('file.js', 'console.log("yes")'); require('./file')
    yes
    {}
    > fs.writeFileSync('file.json', 'console.log("no")'); require('./file.json')
    SyntaxError: file.json: Unexpected token c in JSON at position 0
Re:JavaScript is Good, Actually by gaius on Wednesday May 16, 2018 @05:16PM (#17086570)
This is the community that installs things by curl|sudo bash, remember
Re:JavaScript is Good, Actually by h1d on Wednesday May 16, 2018 @10:03AM (#17082428)
JSON has so many of these little but annoying limitations, I can't believe it's finalized like that.

JSON5 is something that should've been.

https://json5.org

Re:JavaScript is Good, Actually by mschuetz on Wednesday May 16, 2018 @08:52AM (#17081956)
I'm still bitter about JSON not supporting comments.
Re:JavaScript is Good, Actually by h1d on Wednesday May 16, 2018 @10:04AM (#17082436)
That's why I use 'ini' format for config files despite not being standardized.
Re:JavaScript is Good, Actually by adjkant on Wednesday May 16, 2018 @07:49AM (#17081628)
Try Python - JSON works very well with it and while Python has its flaws, I think it's a much better language to use.
Re:JavaScript is Good, Actually by jrs95 on Wednesday May 16, 2018 @09:22AM (#17082142)
Ehhhh...it works okay but the same fundamental serialization problem exists. The fact that dict can be roughly equivalent to JSON helps, but as soon as you start including more complex types you're going to have to use something to handle serialization/deserialization. Which really isn't that complex with Java if you're using a few convenience libraries: gson, orika/mapstruct, lombok all make it a lot less painful to write this kind of boilerplate serialization code.
Re:JavaScript is Good, Actually by chamilto on Wednesday May 16, 2018 @10:01AM (#17082410)
The most common issue I run into is with datetime objects, but there are well documented workarounds. For more complex objects such as ORM objects, I think a lot of people use Marshmallow. That being said, I don't have a lot of experience, so I could be missing something obvious. What issues have you had with Python JSON serialization?
Re:JavaScript is Good, Actually by jrs95 on Wednesday May 16, 2018 @11:59AM (#17083421)
I wouldn't say I've had issues, I just don't think it's really been easier/quicker in Python than most other languages I've used except for the simplest use cases (when Flask jsonify does everything you need because your data is all just strings, numbers, and booleans already)
Re:JavaScript is Good, Actually by kaliades on Wednesday May 16, 2018 @06:23AM (#17081257)
The code completion in JAVA is probably the only way to write java. It is bit too bloated for my taste with all classes and subclasses that handle almost the same thing (speaking mainly from Android perspective)