Active6 years ago
  1. Js Serialize Json
  2. Js Serialize Object

I am using sails.js (node js framework). I am trying to JSON.stringify one of the objects, but when I do that it omits one of the fields (rooms array below). Here is what console.log(object) give. Node.js - How to write/serialize an arbitrary JavaScript object that contains functions and special values and save it to a.js file Ask Question up vote 1 down vote favorite.

Example:

Database table with 2 rows:

An inventory transaction to subtract 5 milk items needs to be executed using the oldest milk first.

e.g.

Select rowid 1wait for its callback and in it Delete rowid 1 as its qty goes to 0 and is therefore spent

New deletion qty = 5 - 3 = 2. Since its not 0, another Select needs to be made.

Select rowid 2

wait for its callback and in it Update rowid 2 to set qty = 38.

Thank you for downloading Firebird PHP Generator Professional from our software portal The download is provided as is, with no modifications or changes made on our side. You are downloading Firebird PHP Generator Professional, version 12.8.0.10. Firebird php generator professional courier. Each download we provide is subject to periodical scanning, but we strongly recommend you check the package for viruses on your side before running the installation. It may sharply differ from the full version of the program due to the license type. You are about to download a trial version of the program.

The serialize capability in the sqlite binding will only serialize the two Selects. There's no way to know the second Select is even needed until the first Select and its callback are finished. It's only then that its realized that the first Select offered an insufficient qty.

How does one serialize the first Select and its callback using pure JavaScript - no JQuery or other libraries.

Here's a pseudo code example:This is the smallest example I can think of to demo the issue.

Assume db is the result of a sqlite database open call which was successful.

The problem is that the while will execute continuously because nothing within its scope changes the value of deletionQty.Only the callback modifies it and since these are async events, the while loop will execute, say 100 times before thecallback changes the value of deletionQty. If there were a 1 to 1 relationship (a synchronous relationship) so that onlyone db.get is executed and then its callback is executed before the next while loop cycle, everything would work fine.It's the async delayed callback that allows the while to execute continuously setting up new callbacks that's the issue.

Bill Gradwohl

Js Serialize Json

Bill GradwohlBill Gradwohl

1 Answer

I've recently created a simple abstraction named WaitFor to call async functions in sync mode (based on Fibers): https://github.com/luciotato/waitfor

your code with wait.for:

WiredPrairie's comment it's the most accurate. You can't turn off async in Node. It's fundamental to Node. You need a lib like wait.for(based on node-fibers), or 'async' or promises (Q) to code sequential logic.

DB sequential logic it's not what's node is made for.

check this answer also:Callback hell in nodejs?

Beyond all this, maybe you need to have a table with: product_id, qyt_in_stockand increment or decrement this field instead of deleting and inserting rows.

Community
Lucio M. TatoLucio M. Tato
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.

Not the answer you're looking for? Browse other questions tagged javascriptnode.jsserializationsqlite or ask your own question.

A Node.js framework agnostic library for (de)serializing your data to JSONAPI (1.0 compliant).

Installation

$ npm install jsonapi-serializer

Documentation

Serialization

The function JSONAPISerializer takes two arguments:

  • type: The resource type.
  • opts: The serialization options.

Calling the serialize method on the returned object will serialize your data (object or array) to a compliant JSONAPI document.

Available serialization option (opts argument)

  • attributes: An array of attributes to show. You can define an attribute as an option if you want to define some relationships (included or not).
    • ref: If present, it's considered as a relationships.
    • included: Consider the relationships as compound document. Default: true.
    • id: Configurable identifier field for the resource. Default: id.
    • attributes: An array of attributes to show.
    • topLevelLinks: An object that describes the top-level links. Values can be string or a function
    • dataLinks: An object that describes the links inside data. Values can be string or a function (see examples below)
    • dataMeta: An object that describes the meta inside data. Values can be a plain value or a function (see examples below)
    • relationshipLinks: An object that describes the links inside relationships. Values can be string or a function
    • relationshipMeta: An object that describes the meta inside relationships. Values can be a plain value or a function
    • ignoreRelationshipData: Do not include the data key inside the relationship. Default: false.
    • keyForAttribute: A function or string to customize attributes. Functions are passed the attribute as a single argument and expect a string to be returned. Strings are aliases for inbuilt functions for common case conversions. Options include: dash-case (default), lisp-case, spinal-case, kebab-case, underscore_case, snake_case, camelCase, CamelCase.
    • nullIfMissing: Set the attribute to null if missing from your data input. Default: false.
    • pluralizeType: A boolean to indicate if the type must be pluralized or not. Default: true.
    • typeForAttribute: A function that maps the attribute (passed as an argument) to the type you want to override. If it returns undefined, ignores the flag for that attribute. Option pluralizeType ignored if set.
    • meta: An object to include non-standard meta-information. Values can be a plain value or a function
    • transform: A function to transform each record before the serialization.

Js Serialize Object

Examples

Simple usage:

The result will be something like:

Deserialization

The function JSONAPIDeserializer takes one argument:

Php 7 vs node js
  • opts: The deserializer options.

Calling the deserialize method on the returned object will deserialize your data (JSONAPI document) to a plain javascript object.

Available deserialization option (opts argument)

  • keyForAttribute: A function or string to customize attributes. Functions are passed the attribute as a single argument and expect a string to be returned. Strings are aliases for inbuilt functions for common case conversions. Options include: dash-case (default), lisp-case, spinal-case, kebab-case, underscore_case, snake_case, camelCase, CamelCase.
  • AN_ATTRIBUTE_TYPE: this option name corresponds to the type of a relationship from your JSONAPI document.
    • valueForRelationship: A function that returns whatever you want for a relationship (see examples below) can return a Promise (see tests)
    • transform: A function to transform each record after the deserialization.

Examples

Simple usage:

Relationship:

Notes on Promises

The deserialization option valueForRelationship supports returning a Promise and so this library uses Promises under the hood. bluebird was previously used as a dependency, but due to bundle size concerns on both node and the web it was replaced with native promises.

bluebird is definitely more performant than native Promises. If performance is a major concern Promise can be globally polyfilled

  • node - via global.Promise = require('bluebird')
  • web - global Promise automatically gets assigned when using the script tag to load bluebird

Error serialization

The function JSONAPIError takes one argument:

  • opts: The error options. All options are optional.

Available error option (opts argument)

  • id: a unique identifier for this particular occurrence of the problem.
  • status: the HTTP status code applicable to this problem, expressed as a string value.
  • code: an application-specific error code, expressed as a string value.
  • title: a short, human-readable summary of the problem that SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localization.
  • detail: a human-readable explanation specific to this occurrence of the problem. Like title, this field’s value can be localized.
  • source: an object containing references to the source of the error, optionally including any of the following members:
    • pointer: a JSON Pointer [RFC6901] to the associated entity in the request document [e.g. '/data' for a primary data object, or '/data/attributes/title' for a specific attribute].
    • parameter: a string indicating which URI query parameter caused the error.
  • links: a links object containing the following members:
    • about: a link that leads to further details about this particular occurrence of the problem.
  • meta: a meta object containing non-standard meta-information about the error.

Examples

Simple usage:

The result will be something like: