Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #400: SON.json(Locale) throw StackOverflowError, into the dead loop #401

Closed

Conversation

takeseem
Copy link
Contributor

@takeseem takeseem commented Mar 14, 2017

在整个dubbo项目中,JSON.json只有两种用途

  1. 记录日志时:pojo->json
  2. CacheFilter中的cache的key生成
Result com.alibaba.dubbo.cache.filter.CacheFilter.invoke(Invoker invoker, Invocation invocation) throws RpcException
    String key = StringUtils.toArgumentString(invocation.getArguments());



	public static String toArgumentString(Object[] args) {
	    StringBuilder buf = new StringBuilder();
        for (Object arg : args) {
            if (buf.length() > 0) {
                buf.append(Constants.COMMA_SEPARATOR);
            }
            if (arg == null || ReflectUtils.isPrimitives(arg.getClass())) {
                buf.append(arg);
            } else {
                try {
                    buf.append(JSON.json(arg));
                } catch (IOException e) {
                    logger.warn(e.getMessage(), e);
                    buf.append(arg);
                }
            }
        }
        return buf.toString();
	}

所以完全可以用string来代替,而且locale.toString()确实能代替Locale

  1. Locale.toString = "${language}${country}${variant}"
  2. str用_分割反序列化为Locale(String language, String country, String variant)

@takeseem
Copy link
Contributor Author

takeseem commented Mar 14, 2017

JSON.json references in the dubbo
_031

StringUtils.java

	public static String toArgumentString(Object[] args) {
	    StringBuilder buf = new StringBuilder();
        for (Object arg : args) {
            if (buf.length() > 0) {
                buf.append(Constants.COMMA_SEPARATOR);
            }
            if (arg == null || ReflectUtils.isPrimitives(arg.getClass())) {
                buf.append(arg);
            } else {
                try {
                    buf.append(JSON.json(arg));
                } catch (IOException e) {
                    logger.warn(e.getMessage(), e);
                    buf.append(arg);
                }
            }
        }
        return buf.toString();
	}

StringUtils.toArgumentString仅仅用在CacheFilter中生成key,所以使用Locale.toString也是没有问题的。这样的String也能完整转换回Locale

@kimmking
Copy link
Member

kimmking commented Jun 8, 2017

其实更简单的办法是,JSON的这一块,都用fastjson重写得了。

@chickenlj chickenlj self-assigned this Oct 23, 2017
@chickenlj
Copy link
Contributor

We've already replaced JSON impl with fastjson

@chickenlj chickenlj closed this Oct 26, 2017
@takeseem
Copy link
Contributor Author

老项目有时候伤不起!依赖关系有时候太多了!

fix see #906,我们主要用的是dubbo协议,hessian不知道能不能自动把:locale的字符串反序列化为Locale对象,我想应该是ok的,jackson支持

public static void main(String[] args) throws IOException {
	ObjectMapper mapper = new ObjectMapper();
	String json = mapper.writeValueAsString(Locale.US);
	System.out.println(json);
	System.out.println(mapper.readValue(json, Locale.class));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants