This question already has answers here:
Convert XML to JSON (and back) using Javascript
(14 answers)
Closed 8 years ago.
I have a server Implementation of ECMA Script including the ability to use E4X. Because this is pretty elegant for people don't know JavaScript and JSON Notation and we want to make an API which is most easy to learn i want to use this for my API.
I'm currently evaluating if i can use this in my environment. One Showstopping feature that i must use is to convert those XML Objects of E4X into JSON compatible JavaScript Objects or Strings on the fly. I can't use XSLT here because i have to stay inside JavaScript.
So the question is, is there an easy way to convert E4X XML Elements into JSON? Or do i have to write some code to convert it myself?
You can use XSLT to convert your XML to JSON.
For instance using: http://code.google.com/p/xml2json-xslt/
However you can end up with a very XMLish and unnecessarily complex JSON. That will make your code more difficult to write and maintain.
An API is generally meant to be stable in time, so may be some dedicated XSLT for each calls may be a better option than a generic one.
Related
I recently found out that JSON values can only store string, number, object, array, true, false or null. But from my understanding, JSON is how Javascript represents its objects internally. I don't understand how it is possible to store Javascript objects as JSON if most objects have methods, which are functions? Aren't functions objects? What the heck are functions in my Javascript interpreter's (Node.js) opinion and how does it represent them? Thanks!
JSON is a string interchange format. It stands for JavaScript Object Notation. It was invented long after Javascript and has nothing at all to do with how Javascript stores data internally.
JSON is typically used as an interchange format or storage format. One would take some Javascript data, serialize it to the JSON format and take the resulting string and send it to another process or computer or save it in some sort of storage.
The recipient of the JSON can then parse it back into whatever their local data is. JSON is even used to send data from a Javascript program to a program written in another language (Python, Ruby, C++, etc...).
Functions have no connection at all to JSON. They are not stored in JSON. Their internal storage format inside the JS interpreter is specific to whatever interpreter implementation and is not accessible to the outside world or governed by any standard. It's an implementation detail for any Javascript engine and they can do it however they want and each interpreter likely has it's own implmentation or variation. I don't know of any reason why it would matter to your Javascript code.
I recently found out that JSON values can only store string, number, object, array, true, false or null. But from my understanding, JSON is how Javascript represents its objects internally.
That is not correct. JSON is not something that the Javascript interpreter uses for its objects internally. Internal object formats are specific to a particular Javascript interpreter and are not accessible to Javascript code, nor really relevant when writing code.
I don't understand how it is possible to store Javascript objects as JSON if most objects have methods, which are functions?
Javascript does not use JSON for internal storage so it has nothing at all to do with the internal implementation of Javascript data types.
Aren't functions objects?
Yes, but they have nothing to do with JSON.
What the heck are functions in my Javascript interpreter's (Node.js) opinion and how does it represent them?
Each JS interpreter has its own internal implementation/storage for functions. It is not governed by any standard and is largely irrelevant to how you write code in Javascript.
If you had some reason to want to know how a specific Javascript implementation stores its variables internally, you would have to look into the source code. The V8 implementation from Google (used in Chrome and node.js) and the Firefox implementation from Mozilla are both open source and you could dive into that code (it would be mostly C++ code).
This can get pretty complicated because some data types such as Arrays are stored in a variety of different formats depending upon the structure of the array. I believe V8 has at least three storage formats for arrays depending upon whether the array is compacted or sparse and based on its overall size. This is to optimize for both memory consumption and run-time performance.
Likewise properties on objects may be arranged in highly optimized storage formats if the interpreter has advance information from the code about what is being used and what is not, compared to arbitrary programmatically generated properties.
FYI, you can find the Google repository here: https://chromium.googlesource.com/v8/v8.git and the Mozilla code here: https://hg.mozilla.org/.
This question already has answers here:
How to add different types of objects in a single array in C#?
(11 answers)
Closed 6 years ago.
I'm putting together a little C# project - I am rewriting something I had written in JavaScript as C#.
Part of my project requires an array of arrays with mixed content in. In JavaScript I just initiate the array:
Log: []
then use push:
Log.push(logEntry);
Where logEntry is:
string, string, string, int, int, int
The ints may also be empty.
The purpose of this is to store data before JSON encoding it and sending it to a web API on another server.
Can someone point me in the right direction to do this in C#, as all the answers I find seem to want to only store the same type of data.
Thanks in advance,
Tony.
Here is how you can do it
Use List<object> (as everything is derived from object in C#):
var list = new List<object>();
list.Add(123);
list.Add("Hello World");
Also dynamic might work for you (and your javascript background)
var list = new List<dynamic>();
list.Add(123);
list.Add(new
{
Name = "Lorem Ipsum"
});
If you wan't to use dynamic you really need to know what you're doing. Please read this MSDN article before you start.
But do you need it?
C# is a strongly-typed and very solid programming language. It is very flexible and great for building apps using object-oriented and functional paradigms. What you want to do may be fine for javascript, but looks a bit ugly with C#. My recommendation is: use object oriented programming and try to build models for your problem. Never mix types together like you tried. One list is for a single data-type.
You can try using the ArrayList class.
Another way for a heterogeneous collection of objects is to use List<Object>
The ArrayList class is designed to hold collections of objects. The objects can be of different Type. However, it does not always offer the best performance.
From MSDN
The ArrayList class is designed to hold heterogeneous collections of
objects. However, it does not always offer the best performance.
Instead, we recommend the following:
For a heterogeneous collection of objects, use the List (in
C#) or List(Of Object) (in Visual Basic) type.
For a homogeneous collection of objects, use the List class.
This question already has answers here:
How to store a byte array in Javascript
(4 answers)
Closed 7 years ago.
The ByteArray class provides methods and properties to optimize reading, writing, and working with binary data.
How to use byte arrays tutorial.
I'm looking for a very similar API as the one linked.
I'm looking for a class the browser provides not hack or workaround. The linked question does not provide the answer. If it does please provide a link to the documentation.
Some one linked to another question but that did not answer my question.
Update: Someone off list pointed me to this class:
https://gist.github.com/sunetos/275610#file-bytearray-js
It has most or all of the read methods but none of the write methods and it's not native to the browser.
Modern browsers support Uint8Array, one of JavaScript's TypedArray classes.
var data = new Uint8Array(8);
var data = new Uint8Array([0x10, 0x12]);
It does not have built-in methods for encoding/decoding Unicode strings. See Converting between strings and ArrayBuffers for examples of how to do that.
The answer is yes, here are the relevant docs since you just wanted this...
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays
The linked docs for each type in the above docs show the methods available on each type. IE: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int8Array
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to parse JSON in Android
I am writing an Android app where I'm getting a .js (JavaScript) file from a url and I want to read its contents. Is there any way to convert this file to a JSONArray or JSONObject? Or a direct way to parse the .js file itself?
If you mean can you create a data structure out of raw JavaScript, then no.
You can't convert a string of arbitrary JavaScript to JSON except in the degenerate sense that the entire string is a valid JSON item of type 'string'. This is because the syntax of JS covers a much larger domain than JSON. For example, what kind of JSON structure would you expect to represent the following JS?
while (true) { }
If your intent is to traverse the JS and pull data structures out of it, you're probably going to need something like a full JavaScript parsing engine.
If on the other hand you've phrased the question badly and the '.js' file you're fetching is really a JSON file, then the question is answered in the marked duplicate.
This question already has answers here:
Where can I find javascript native functions source code? [duplicate]
(3 answers)
Javascript native sort method code
(3 answers)
Closed 8 years ago.
Is there a way to know how test (for regex) or cos (for math) or substring (for string) or splice (for array) have been coded in the exact syntax either by a way in Javascript (like alert(function)) or by seeking in the files that come with Firefox?
Well, the source code of javascript likely isn't written in javascript itself, so you would be unable to just print it to the browser console or alert.
You can browse the source code of google's V8 engine, and see if it helps you ;)
http://code.google.com/p/v8/
These functions are actually implemented differently by browsers in one of the OS native languages.
So these algorithms are written in c, c++, c#, java etc. but not in JavaScript.