Created
August 24, 2013 18:22
-
-
Save raphaelbauer/6329621 to your computer and use it in GitHub Desktop.
Ninja Framwork - Just a helper class that bypasses the built-in Json serializer and helps rendering the exact stuff you put as "string". Bypasses all built-in json serializers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package conf; | |
import java.io.OutputStream; | |
import ninja.Context; | |
import ninja.Renderable; | |
import ninja.Result; | |
import ninja.utils.ResponseStreams; | |
public class ResultJsonCustom { | |
/** | |
* | |
* Just a helper class that bypasses the built-in Json serializer and helps | |
* rendering the exact stuff you put as "string". | |
* | |
* You are responsible to make sure you are providing a "valid" json as string. | |
* | |
* @param string This string will be rendered with application/json. | |
* @return The result that will render an arbitrary custom json using a string. | |
*/ | |
public static Result render(final String string) { | |
Result result = new Result(200); | |
result.contentType(Result.APPLICATON_JSON); | |
Renderable renderable = new Renderable() { | |
@Override | |
public void render(Context context, Result result) throws Exception { | |
ResponseStreams resultJsonCustom = context | |
.finalizeHeaders(result); | |
OutputStream outputStream = resultJsonCustom.getOutputStream(); | |
outputStream.write(string.getBytes()); | |
outputStream.close(); | |
} | |
}; | |
result.render(renderable); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment