Giter Club home page Giter Club logo

json's People

Contributors

3imed-jaberi avatar dependabot[bot] avatar jonathanong avatar nicolasgramlich avatar tj avatar wesleyclements avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

json's Issues

Custom JSON serializer (for BigInt) [feature request]

Is it possible to customize the serializer?
My applications use BigInt and V8 (just as Chrome) throws a nice:

  TypeError: Do not know how to serialize a BigInt
      at JSON.stringify (<anonymous>)
      at next.then (/Users/damz/Desktop/cfciampino-reloaded/node_modules/koa-json/index.js:50:32)

I'd like to apply a workaround like (see next) when returning my payloads:
tc39/proposal-bigint#146 (comment)

Good jobs guys!

Prettify automatically based on accept header

It would be nice if there was an option for the prettify to happen automatically based on an accept header, something like this:

switch (ctx.accepts('json', 'html', 'text')) {
    json: break;
    html:
    text:
        // pretty print json here
    default: break;
}

[bug] string payload is not json encoded

The following results in a payload hola and not the json "hola".

import http from 'http';
import koa from 'koa';
import json from 'koa-json';

const app = new koa();
app.use(json());

app.use(ctx => ctx.body = 'hola');

http.createServer(app.callback()).listen(1234);

Current workaround:

app.use(async (ctx, next) => {
    await next();

    if (typeof ctx.body === 'string')
        ctx.body = `"${ctx.body}"`;
});

`this.body` throw Error: Can't remove headers after they are sent.

Hello,

koa is a middleware in my scene, I use koa to send HTTP request and then reponse to browser.

here is my code:

var reqHttp = http.request(opt, function(resHttp){
    if (resHttp.statusCode == 200){
        var buf = "";
        resHttp.on('data', function(data) {
            buf += data;
        });
        resHttp.on('end', function(){
            ctx.body = JSON.parse(buf); // throw error: Can't remove headers after they are sent.
        });
    } else {
        ctx.body = {error: -1};
    }
});
reqHttp.write(data);
reqHttp.end();

I just want a like res.json in express for koa...

please help me ?

not working at all (because this.body is already stringifed json?)

Module doesn't seem to be working at all for me.

...
app.use(require('koa-json')());

router.get('/test', function *(next) {
  this.body = {test: 'testing'};
});
...

... go to /test, doesn't look pretty.

Digging into the code I see that it doesn't print pretty if this.body is a string, but by the time koa-json code is run JSON seems to have been stringified already. Is my usage wrong or has koa changed how/when it stringifies JSON which means koa-json no longer works?

Empty response when result contains null or undefined values

If route returns null or undefined or an object with a null or undefined property then body/response is empty!

app.use(async (ctx, next) => {
	try {
    	      await next();
    	      ctx.body = null; //empty response
    	      ctx.body = undefined; //empty response
    	      ctx.body = { data: null }; //empty response
    	      ctx.body = { data: undefined }; //empty response
  	}
	catch(error) {
	    	ctx.status = 400;
		ctx.body = error.message || error;
	}
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.