Here you can find the source of convertString2GSonString(String str)
public static String convertString2GSonString(String str)
//package com.java2s; /******************************************************************************* * Copyright (c) 2015 Frank Becker and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/*from www.ja v a 2s. c o m*/ * Frank Becker - initial API and implementation *******************************************************************************/ public class Main { public static String convertString2GSonString(String str) { str = str.replace("\"", "\\\"").replace("\n", "\\\n"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$//$NON-NLS-4$ StringBuffer ostr = new StringBuffer(); for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if ((ch >= 0x0020) && (ch <= 0x007e)) { ostr.append(ch); } else { ostr.append("\\u"); //$NON-NLS-1$ String hex = Integer.toHexString(str.charAt(i) & 0xFFFF); for (int j = 0; j < 4 - hex.length(); j++) { ostr.append("0"); //$NON-NLS-1$ } ostr.append(hex.toLowerCase()); } } return (new String(ostr)); } }