<commit_before>package cn.jiguang.cordova.im;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.content.CustomContent;
import cn.jpush.im.android.api.content.EventNotificationContent;
import cn.jpush.im.android.api.content.FileContent;
import cn.jpush.im.android.api.content.ImageContent;
import cn.jpush.im.android.api.content.LocationContent;
import cn.jpush.im.android.api.content.MessageContent;
import cn.jpush.im.android.api.content.TextContent;
import cn.jpush.im.android.api.content.VoiceContent;
import cn.jpush.im.android.api.enums.ConversationType;
import cn.jpush.im.android.api.enums.MessageDirect;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.GroupInfo;
import cn.jpush.im.android.api.model.Message;
import cn.jpush.im.android.api.model.UserInfo;

class JsonUtils {

    static Map<String, String> fromJson(JSONObject jsonObject) throws JSONException {
        Map<String, String> map = new HashMap<String, String>();

        Iterator<String> keysItr = jsonObject.keys();
        while (keysItr.hasNext()) {
            String key = keysItr.next();
            String value = jsonObject.getString(key);
            map.put(key, value);
        }
        return map;
    }

    static JSONObject toJson(Map<String, String> map) {
        Iterator<String> iterator = map.keySet().iterator();

        JSONObject jsonObject = new JSONObject();
        while (iterator.hasNext()) {
            String key = iterator.next();

            try {
                jsonObject.put(key, map.get(key));
            } catch (JSONException e) {
                Log.wtf("RequestManager", "Failed to put value for " + key + " into JSONObject.", e);
            }
        }
        return jsonObject;
    }

    static JSONObject toJson(final UserInfo userInfo) {
        if (userInfo == null) {
            return null;
        }

        final JSONObject result = new JSONObject();
        try {
            result.put("type", "user");
            result.put("gender", userInfo.getGender());
            result.put("username", userInfo.getUserName());
            result.put("appKey", userInfo.getAppKey());
            result.put("nickname", userInfo.getNickname());

            if (userInfo.getAvatarFile() != null) {
                result.put("avatarThumbPath", userInfo.getAvatarFile().getAbsolutePath());
            } else {
                result.put("avatarThumbPath", "");
            }

            result.put("birthday", userInfo.getBirthday());
            result.put("region", userInfo.getRegion());
            result.put("signature", userInfo.getSignature());
            result.put("address", userInfo.getAddress());
            result.put("noteName", userInfo.getNotename());
            result.put("noteText", userInfo.getNoteText());
            result.put("isNoDisturb", userInfo.getNoDisturb() == 1);
            result.put("isInBlackList", userInfo.getNoDisturb() == 1);
            result.put("isFriend", userInfo.isFriend());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return result;
    }

    static JSONObject toJson(GroupInfo groupInfo) {
        JSONObject result = new JSONObject();

        try {
            result.put("type", "group");
            result.put("id", groupInfo.getGroupID());
            result.put("name", groupInfo.getGroupName());
            result.put("desc", groupInfo.getGroupDescription());
            result.put("level", groupInfo.getGroupLevel());
            result.put("owner", groupInfo.getGroupOwner());
            result.put("ownerAppKey", groupInfo.getOwnerAppkey());
            result.put("maxMemberCount", groupInfo.getMaxMemberCount());
            result.put("isNoDisturb", groupInfo.getNoDisturb() == 1);
            result.put("isBlocked", groupInfo.isGroupBlocked() == 1);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return result;
    }

    static JSONObject toJson(Message msg) {
        JSONObject result = new JSONObject();
        try {
            result.put("id", msg.getId());
            result.put("from", toJson(msg.getFromUser()));

            if (msg.getDirect() == MessageDirect.send) {
                if (msg.getTargetType() == ConversationType.single) {
                    result.put("target", toJson((UserInfo) msg.getTargetInfo()));
                } else if (msg.getTargetType() == ConversationType.group) {
                    result.put("target", toJson((GroupInfo) msg.getTargetInfo()));
                }

            } else {
                UserInfo myInfo = JMessageClient.getMyInfo();
                result.put("target", toJson(myInfo));<commit_msg>Fixed message target info error.<commit_after>package cn.jiguang.cordova.im;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.content.CustomContent;
import cn.jpush.im.android.api.content.EventNotificationContent;
import cn.jpush.im.android.api.content.FileContent;
import cn.jpush.im.android.api.content.ImageContent;
import cn.jpush.im.android.api.content.LocationContent;
import cn.jpush.im.android.api.content.MessageContent;
import cn.jpush.im.android.api.content.TextContent;
import cn.jpush.im.android.api.content.VoiceContent;
import cn.jpush.im.android.api.enums.ConversationType;
import cn.jpush.im.android.api.enums.MessageDirect;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.GroupInfo;
import cn.jpush.im.android.api.model.Message;
import cn.jpush.im.android.api.model.UserInfo;

class JsonUtils {

    static Map<String, String> fromJson(JSONObject jsonObject) throws JSONException {
        Map<String, String> map = new HashMap<String, String>();

        Iterator<String> keysItr = jsonObject.keys();
        while (keysItr.hasNext()) {
            String key = keysItr.next();
            String value = jsonObject.getString(key);
            map.put(key, value);
        }
        return map;
    }

    static JSONObject toJson(Map<String, String> map) {
        Iterator<String> iterator = map.keySet().iterator();

        JSONObject jsonObject = new JSONObject();
        while (iterator.hasNext()) {
            String key = iterator.next();

            try {
                jsonObject.put(key, map.get(key));
            } catch (JSONException e) {
                Log.wtf("RequestManager", "Failed to put value for " + key + " into JSONObject.", e);
            }
        }
        return jsonObject;
    }

    static JSONObject toJson(final UserInfo userInfo) {
        if (userInfo == null) {
            return null;
        }

        final JSONObject result = new JSONObject();
        try {
            result.put("type", "user");
            result.put("gender", userInfo.getGender());
            result.put("username", userInfo.getUserName());
            result.put("appKey", userInfo.getAppKey());
            result.put("nickname", userInfo.getNickname());

            if (userInfo.getAvatarFile() != null) {
                result.put("avatarThumbPath", userInfo.getAvatarFile().getAbsolutePath());
            } else {
                result.put("avatarThumbPath", "");
            }

            result.put("birthday", userInfo.getBirthday());
            result.put("region", userInfo.getRegion());
            result.put("signature", userInfo.getSignature());
            result.put("address", userInfo.getAddress());
            result.put("noteName", userInfo.getNotename());
            result.put("noteText", userInfo.getNoteText());
            result.put("isNoDisturb", userInfo.getNoDisturb() == 1);
            result.put("isInBlackList", userInfo.getNoDisturb() == 1);
            result.put("isFriend", userInfo.isFriend());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return result;
    }

    static JSONObject toJson(GroupInfo groupInfo) {
        JSONObject result = new JSONObject();

        try {
            result.put("type", "group");
            result.put("id", groupInfo.getGroupID());
            result.put("name", groupInfo.getGroupName());
            result.put("desc", groupInfo.getGroupDescription());
            result.put("level", groupInfo.getGroupLevel());
            result.put("owner", groupInfo.getGroupOwner());
            result.put("ownerAppKey", groupInfo.getOwnerAppkey());
            result.put("maxMemberCount", groupInfo.getMaxMemberCount());
            result.put("isNoDisturb", groupInfo.getNoDisturb() == 1);
            result.put("isBlocked", groupInfo.isGroupBlocked() == 1);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return result;
    }

    static JSONObject toJson(Message msg) {
        JSONObject result = new JSONObject();
        try {
            result.put("id", msg.getId());
            result.put("from", toJson(msg.getFromUser()));  // 消息发送者

            if (msg.getDirect() == MessageDirect.send) {    // 消息发送
                if (msg.getTargetType() == ConversationType.single) {   // 消息发送对象的类型
                    result.put("target", toJson((UserInfo) msg.getTargetInfo()));
                } else if (msg.getTargetType() == ConversationType.group) {
                    result.put("target", toJson((GroupInfo) msg.getTargetInfo()));
                }

            } else if (msg.getDirect() == MessageDirect.receive) {    // 消息接收
                if (msg.getTargetType() == ConversationType.single) {
                    result.put("target", toJson(JMessageClient.getMyInfo()));
                } else if (msg.getTargetType() == ConversationType.group) {
                    result.put("target", toJson((GroupInfo) msg.getTargetInfo()));
                }
{'fraction_non_alphanumeric': 0.1371007371007371, 'fraction_numerical': 0.0007862407862407862, 'mean_word_length': 2.4436548223350254, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5870282', 'n_tokens_mistral': 2959, 'n_tokens_neox': 2896, 'n_words': 505}