def create_literal(val, datatype=None, lang=None):
    """
    Put the value in unicode escaping new special characters to keep canonical form
    From NT specification at 'https://www.w3.org/TR/n-triples/#canonical-ntriples':
    "Within STRING_LITERAL_QUOTE, only the characters U+0022, U+005C, U+000A, U+000D are
    encoded using ECHAR. ECHAR must not be used for characters that are allowed directly
    in STRING_LITERAL_QUOTE."
    """
    escape_pairs = [(u'\\', '\\\\'), (u'"', '\\"'), (u'\n', '\\n'), (u'\r',
        '\\r')]
    for orig, rep in escape_pairs:
        val = val.replace(orig, rep)
    lit_value = u'"{}"'.format(val)
    if datatype is not None:
        lit_value += '^^<{}>'.format(datatype)
    elif lang is not None:
        lit_value += '@{}'.format(lang)
    return lit_value
